Howto Iterate thru a control collection ?

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

I've got a webform and many controls on it. I would like to iterate thru the
control collection to get all the textboxes control to let me change the text
property. Here is my code...for unknown reason, this loop iterates only 3
times! Help please! Thanks!

Dim Control As Web.UI.Control
Dim TextBox As System.Web.UI.WebControls.TextBox

For Each Control In Controls
If TypeOf (Control) Is System.Web.UI.WebControls.TextBox Then
TextBox = CType(Control, TextBox)
TextBox.Text = ""
End If
Next
 
Simon said:
I've got a webform and many controls on it. I would like to iterate thru
the
control collection to get all the textboxes control to let me change the
text
property. Here is my code...for unknown reason, this loop iterates only 3
times! Help please! Thanks!

By any chance are any of your controls contained in other controls? If so,
you will have to modify the code to something like this:

ClearTextBoxes(Me.Controls)

Public Sub ClearTextBoxes(ControlCollection controls)
Dim Control As Web.UI.Control
Dim TextBox As System.Web.UI.WebControls.TextBox

For Each Control In controls
If TypeOf (Control) Is System.Web.UI.WebControls.TextBox Then
TextBox = CType(Control, TextBox)
TextBox.Text = ""
Else
ClearTextBoxes(Control.Controls)
End If
Next
End Sub
 
Simon,

The find does the trick

\\\
Dim frm As Control = Me.FindControl("Form1")
For Each ctl as Control In frm.Controls
Dim tb As TextBox
If TypeOf ctl Is TextBox Then
tb = DirectCast(ctl, TextBox)
tb.Text = ""
End If
Next
///

I hope this helps,

Cor
 
Back
Top