473,804 Members | 3,790 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

looping through all webcontrols in htmlform

Hi. I'm trying to loop through all webcontrols in my
System.Web.UI.H tmlControls.Htm lForm.

This is my code:
Dim form As HtmlForm
Dim control As Control

form = Me.FindControl( "completion ")
For Each control In form.Controls
....
Next

Looking at the form.Controls.C ount property it says that there are 109
controls on my page, but the For Each statement only does one loop where it
returns a LiteralControl. Am I missing something here? Shouldn't the loop
execute 108 times??

Thanks,
Shawn
Nov 18 '05 #1
2 4230
You have to do this recursively. Your form only has 1 control, which has 4
controls, which each have 7 controls...and so on.

private sub recurse(byval c as Control)
for each child in c.Controls
if child .HasChildren then
recurse(child)
end if
end sub

Karl
"Shawn" <Sh***@discussi ons.microsoft.c om> wrote in message
news:CB******** *************** ***********@mic rosoft.com...
Hi. I'm trying to loop through all webcontrols in my
System.Web.UI.H tmlControls.Htm lForm.

This is my code:
Dim form As HtmlForm
Dim control As Control

form = Me.FindControl( "completion ")
For Each control In form.Controls
...
Next

Looking at the form.Controls.C ount property it says that there are 109
controls on my page, but the For Each statement only does one loop where it returns a LiteralControl. Am I missing something here? Shouldn't the loop
execute 108 times??

Thanks,
Shawn

Nov 18 '05 #2
Hi,

Remember, some of those controls contain controls themselves. Your code
sample doesn't recurse the control tree, it just iterates the top level
parent controls. You must write a recursive function similar to the
following (c#):

private void RecurseControls (ControlCollect ion controls)
{
foreach(Control c in controls)
{
//do something with control...

//recurse
if(c.HasControl s())
RecurseControls (c.Controls);
}
}

Or in VB.NET:

Private Sub RecurseControls (ByVal controls As ControlCollecti on)
For Each control As Control In controls
'do something with control

'recurse
If control.HasCont rols() Then
RecurseControls (control.Contro ls)
End If
Next
End Sub

--
Ben
http://bitarray.co.uk/ben


"Shawn" <Sh***@discussi ons.microsoft.c om> wrote in message
news:CB******** *************** ***********@mic rosoft.com...
Hi. I'm trying to loop through all webcontrols in my
System.Web.UI.H tmlControls.Htm lForm.

This is my code:
Dim form As HtmlForm
Dim control As Control

form = Me.FindControl( "completion ")
For Each control In form.Controls
...
Next

Looking at the form.Controls.C ount property it says that there are 109
controls on my page, but the For Each statement only does one loop where
it
returns a LiteralControl. Am I missing something here? Shouldn't the loop
execute 108 times??

Thanks,
Shawn

Nov 18 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
16363
by: Hai Nguyen | last post by:
I have another question. I'm trying to loop through all the textboxes on a web application. The snippet is below //foreach(WebControl ctr in Page.Controls) foreach(Control ctr in Page.Controls) { if(ctr is TextBox) { TextBox t = (TextBox)ctr; t.BackColor = Color.AliceBlue;
1
22557
by: Kelvin | last post by:
Hi All, I wrote a program insert new row between rows while the datatable is looping. It display the error message. Exception Details: System.ArgumentException: This row already belongs to this table. System.Data.DataTable.InsertRow(DataRow row, Int32 proposedID, Int32 pos) +319 System.Data.DataRowCollection.Add(DataRow row) +14
2
1505
by: Jim Heavey | last post by:
Hello, I am trying to figure out how to get to the "footer" control in a datagrid. I set up the following logic: Dim cntrl As Control Dim cnt As Integer = dgNewMovies.Controls.Count For Each cntrl In dgNewMovies.Controls Dim cntrlName As String = cntrl.ID If TypeOf cntrl Is System.Web.UI.WebControls.DataGridItem Then Dim item As System.Web.UI.WebControls.DataGridItem = CType(cntrl,
0
283
by: VB Programmer | last post by:
I have 2 webforms. When I print out the page.Controls(1).tostring variable one says "HtmlForm", the other "HtmlGenericControl". How can I change the first one to "HtmlForm"? ? page.Controls(1).tostring "System.Web.UI.HtmlControls.HtmlForm" ? page.Controls(1).tostring "System.Web.UI.HtmlControls.HtmlGenericControl"
4
1311
by: Nick | last post by:
Hi - I'm trying to loop through the textboxes on a page to unlock them for editing. The Enabled property is set to false, and the following code is on an 'Edit' button on the form. Can anyone tell me why it doesn't work? Thanks Nick. Code:
14
8165
by: Laser Lu | last post by:
Hello, All, I just want to know that generally how to get the HtmlForm element in an ASP.NET Page? Can anybody help? Please:) Best regards, Laser Lu
7
1220
by: Jim Bancroft | last post by:
Hi everyone, I'm struggling with something.....I'd like to loop over all of my page's HyperLink controls, and I'm not sure how to do it. Here's what I'm looking for, in pseudocode: foreach (System.Web.UI.WebControls.HyperLink hLink in this.Controls) {
0
1639
by: Bruno | last post by:
Hello guys, I'm having problem using tagMapping with HtmlForm, I have this in web.config: <tagMapping> <add tagType="System.Web.UI.HtmlControls.HtmlForm" mappedTagType="CustomForm" /> </tagMapping> It just doesn't work... it compiles ok, but the HtmlForm is not mapped to
4
5766
by: adiel_g | last post by:
I am trying to loop through a repeater to retrieve a dataitem field but am getting a NullReferenceException. I can find a checkbox control but cannot find a dataitem field. Here is the code that is looping through the repeater: Dim rc As RepeaterItemCollection = rptReport.Items Dim ri As RepeaterItem Dim chkSelected As System.Web.UI.WebControls.CheckBox Dim customer As String
0
9576
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10323
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10311
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9138
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7613
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6847
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5516
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
4292
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3813
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.