473,406 Members | 2,745 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,406 software developers and data experts.

looping through all webcontrols in htmlform

Hi. I'm trying to loop through all webcontrols in my
System.Web.UI.HtmlControls.HtmlForm.

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.Count 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 4218
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***@discussions.microsoft.com> wrote in message
news:CB**********************************@microsof t.com...
Hi. I'm trying to loop through all webcontrols in my
System.Web.UI.HtmlControls.HtmlForm.

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.Count 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(ControlCollection controls)
{
foreach(Control c in controls)
{
//do something with control...

//recurse
if(c.HasControls())
RecurseControls(c.Controls);
}
}

Or in VB.NET:

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

'recurse
If control.HasControls() Then
RecurseControls(control.Controls)
End If
Next
End Sub

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


"Shawn" <Sh***@discussions.microsoft.com> wrote in message
news:CB**********************************@microsof t.com...
Hi. I'm trying to loop through all webcontrols in my
System.Web.UI.HtmlControls.HtmlForm.

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.Count 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
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)...
1
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...
2
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...
0
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"? ?...
4
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...
14
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
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...
0
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" />...
4
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...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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...
0
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...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
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,...
0
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...

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.