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 Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Looping through controls

Status
Not open for further replies.

nbruckelmyer

Programmer
Joined
May 28, 2003
Messages
6
Location
US
I am trying to loop through my controls on my web page, and perform some operation on the textboxes. This don't seem to be working.

Thanks.

Dim oCurr As Object
'-Loop through each of the controls on the form
For Each oCurr In Me.Controls()
'- Check to see if the control is a text box
If TypeOf oCurr Is TextBox Then
'-If so, toggle the properties
If bEnable Then
oCurr.BackColor = System.Drawing.Color.White
Else
oCurr.backcolor = System.Drawing.Color.Gray
End If
End If
Next
 
Hmm
That should essentially work.
One question - where does bEnable come from? I'm suspecting it is just a regular module level variable. In that case, remember that you'll need to use a viewstate or session variable because asp.net is stateless.



Mark [openup]
 
Try Me.Page.Controls.

I've had to do similar things, and from what I remember the last time, the Page object was the container for the controls (Me <> Page).

Give it a try, and let us know if it works out.

D

p.s. You could also write a recursive sub that just takes a control, sees if it has any controls in its control collection, and if it does check if its a textbox or not. We did this so that if a text box was on teh page, but was part of another control (like a datagrid or a panel), we could still get to it. But see if the me.page.controls works first, then we can talk about his further.
 
I think I figured it out. I had to loop through the Page Controls then the child controls of each page control.

Check it out.

Dim oCurr As Object
Dim c As Control
Dim childc As Control
For Each c In Page.Controls
For Each childc In c.Controls
If TypeOf childc Is TextBox Then
If bEnable Then
oCurr = childc
oCurr.backcolor = System.Drawing.Color.White
Else
oCurr = childc
oCurr.backcolor = System.Drawing.Color.Gray
End If
End If
Next
Next
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top