Iterating trough controls

S

Sandra Castellanos

I want to know if there is a way to easily iterate trough all the controls
contained, say, in a table, including the controls that can be inside the
cells and in any nested tables that are inside?

Regards,

Sandra
 
R

Rick Spiewak

Here is an example which you could expand on. All controls inherit from
Control, so you can walk as far as you want before you decide what kind of
control it is. This could be re-factored to be recursive if it made sense in
your application.

Dim tbl As HtmlTable

For Each c As Control In Me.Controls
If c.GetType Is GetType(HtmlTable) Then
tbl= c
Exit For
End If
Next
For Each c As Control In tbl.Controls
Select Case c.gettype.tostring

Case "System.Web.UI.WebControls.TextBox"
..
..
..
for each c1 as control in c.controls
 
K

Kevin Spencer

You can write a recursive function that takes a System.Web.UI.Control as an
argument, and searches (loops) through its' Child Controls Collection for
whatever it is you're trying to achieve. If it doesn't find what it is
looking for, it calls itself for each Child Control, passing the Child
Control to the instance of the function. This will in essence, "drill down"
into the entire Child Controls hierarchy under a given Control.

--
HTH,
Kevin Spencer
..Net Developer
Microsoft MVP
Big things are made up
of lots of little things.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,768
Messages
2,569,574
Members
45,049
Latest member
Allen00Reed

Latest Threads

Top