Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations wOOdy-Soft on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

how to loop through a webform to change the text property from control

Status
Not open for further replies.

ASPnetNovice

Programmer
Apr 28, 2005
19
US
Hello,

I will like to ask if someone knows how to loop through all of the controls of a webform and populate the text boxes with a value like "hello world" .

This is the code that I am trying but it doesn work


Dim child As Control

For Each child In Me.Controls

If (child.GetType().ToString().Equals ("System.Web.UI.WebControls.TextBox")) Then

Dim tb As TextBox = CType(child, TextBox)
tb.Text = "hello world"


End If

Next

The loop (Each child In Me.Controls) is not looping through all my controls as I am expecting

I will apreciate any help
 
That should work if all of your controls belong to the form but more than likely some controls will not belong to the form (for example, they may belong to a panel that you have on your form). To loop through these controls you would have to write a recursive function to loop through each Sub Control.

Also, you can use this:
Code:
If TypeOf child Is TextBox Then
instead of:
Code:
If (child.GetType().ToString().Equals   ("System.Web.UI.WebControls.TextBox")) Then


--------------------------------------------------------------------------------------------------------------------------------------------

Need help finding an answer?

Try the search facility ( or read FAQ222-2244 on how to get better results.
 
All of my control are directly in the form and I am not using panels.

I don't think the loop works as it is because I have 17 controls in my form and when I run the following code I get a count of 3.

===============================
Dim child As Control
dim counter as integer

For Each child In Me.Controls

counter+=1
lbl.text=counter.tostring

Next


 
Panels was just an example. The reason you are getting a count of 3 and not 17 is that not all controls belong to your form.

--------------------------------------------------------------------------------------------------------------------------------------------

Need help finding an answer?

Try the search facility ( or read FAQ222-2244 on how to get better results.
 
I place all of my controls in the same webform1 so how come they don't belong to the same form ?
 
Because your controls will actually belong to the Form. For example, if you add this to your page load:
Code:
        For Each x As Control In Me.Controls
            Response.Write(x.ID & "<br>")
        Next
You'll notice that it probably writes out Form1 (if that is the name of your form).

Therefore all your controls belong to Form1 and you'll need to do the recursive function I talked about earlier.


--------------------------------------------------------------------------------------------------------------------------------------------

Need help finding an answer?

Try the search facility ( or read FAQ222-2244 on how to get better results.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top