Iterate the Controls Collection
Skip Navigation Links.

Iterate the Controls Collection

Skip Navigation LinksHome :: ASP.NET :: Controls :: Iterating Controls

There are times you need to know what controls are on your page. You might test them for example on page init or page load.
This is how you print out all the controls on the page.
Press Submit to see what controls are on this page - if you try entering some text below or checking the checkbox, you will see the control values too




Public Sub CheckControls(ByVal Container As Object)

'call like this CheckControls(Page) to iterate through all controls (or just pass in a more specific object)

'write straight to page - bit old fashioned - could build a string and set a label.text

Response.Write("<br>" & Container.ID & " controls are ... <br>")

Dim ctl As Control

For Each ctl In Container.Controls

Response.Write("&nbsp;&nbsp;&nbsp;") 'indent

If ctl.HasControls Then 'be thorough, iterate child controls

CheckControls(ctl)

End If

If TypeOf ctl Is CheckBox Then 'pick out specific types and display properties of interest

Dim ChkBox As CheckBox

ChkBox = ctl

Response.Write("<b>ChkBox=</b>" & ChkBox.ID & " checked=" & ChkBox.Checked)

ElseIf TypeOf ctl Is TextBox Then 'show it's ID and text values

Dim TxtBox As TextBox

TxtBox = ctl

Response.Write("<b>TextBox=</b>" & TxtBox.ID & " text=" & TxtBox.Text)

Else 'just show it's ID

'Response.Write(ctl.ID & "|")

End If

Next

End Sub

Here's the code...

     Public Sub CheckControls(ByVal Container As Object)
        'call like this CheckControls(Page) to iterate through all controls (or just pass in a more specific object)
								'write straight to page - bit old fashioned - could build a string and set a label.text
		Response.Write("
")
        Dim ctl As Control
        For Each ctl In Container.Controls
			If ctl.HasControls Then	'be thorough, iterate child controls
				CheckControls(ctl)
			End If
			If TypeOf ctl Is CheckBox Then 'pick out specific types and display properties of interest
				Response.Write(Container.ID & " controls are ... 
") 'indent Dim ChkBox As CheckBox ChkBox = ctl Response.Write("ChkBox=" & ChkBox.ID & " checked=" & ChkBox.Checked) Response.Write(vbCrLf) ElseIf TypeOf ctl Is TextBox Then 'show it's ID and text values Response.Write(Container.ID & " controls are ...
") 'indent Dim TxtBox As TextBox TxtBox = ctl Response.Write("TextBox=" & TxtBox.ID & " text=" & TxtBox.Text) Response.Write(vbCrLf) Else 'just show it's ID 'Response.Write(ctl.ID & "|") End If Next Response.Write("
") End Sub
Next >> listbox
top of page
all content ©1996/2012 BennySutton.com
all images © their respective owners This site uses Thumbshots previews