"Michael Beck" <Sp*************@pacbell.net> wrote in message news:%2****************@TK2MSFTNGP12.phx.gbl...
I am new to .net programming and I want to know if I can loop thru a
specified number of controls on an html form.
' Someplace within the WebForm1 class, most likely in Page_Load.
'
Dim c As System.Web.UI.Control
For Each c In Me.Controls
' Do something with c
Next c
This will go through all of the top level controls on the web form,
but not nested controls (each c will have it's own Controls collection
containing it's children, so you can choose to descend recursively if
you so choose).
Public Sub DescendThroughControls( ByVal controlSet As System.Web.UI.ControlCollection )
Dim c As System.Web.UI.Control
For Each c In controlSet
' Do something with c.
If ( c.Controls.Count > 0 ) Then
' This step will do something with c's child controls.
DescendThroughControls( c.Controls )
End If
Next c
End Sub
' . . .
' called with the following statement in Page_Load
' . . .
Me.DescendThroughControls( Me.Controls)
Finally, if you're looking for a Control on the Page with a specific ID then you
can use Me.FindControl( name ) to return an instance of that Control to do
something with. Again, it only works within your immediate ControlCollection
so you may need to descend into the child control collections.
Derek Harmon