471,349 Members | 1,127 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,349 software developers and data experts.

Howto Iterate thru a control collection ?

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
Jul 21 '05 #1
2 5203
"Simon" <Si***@discussions.microsoft.com> wrote in message
news:CC**********************************@microsof t.com...
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

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

Jul 21 '05 #2
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
Jul 21 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

2 posts views Thread by JDavies | last post: by
1 post views Thread by Dan Sikorsky | last post: by
1 post views Thread by Dean Slindee | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.