"Michael Beck" <Sp************ *@pacbell.net> wrote in message news:%2******** ********@TK2MSF TNGP12.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.C ontrol
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 DescendThroughC ontrols( ByVal controlSet As System.Web.UI.C ontrolCollectio n )
Dim c As System.Web.UI.C ontrol
For Each c In controlSet
' Do something with c.
If ( c.Controls.Coun t > 0 ) Then
' This step will do something with c's child controls.
DescendThroughC ontrols( c.Controls )
End If
Next c
End Sub
' . . .
' called with the following statement in Page_Load
' . . .
Me.DescendThrou ghControls( 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 ControlCollecti on
so you may need to descend into the child control collections.
Derek Harmon