Iterating trough controls

  • Thread starter Thread starter Sandra Castellanos
  • Start date Start date
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
 
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
 
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.
 
Back
Top