Howto Iterate thru a control collection ?

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
 
S

Sean Hederman

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
 
C

Cor Ligthert

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top