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

Get Controls From Page 1

Status
Not open for further replies.

jfrost10

Programmer
Jun 3, 2001
2,004
CA
Hey guys,

Does anyone know an easier way to cycle through controls on a page without having to use recursion?

i.e. I want to set all my textboxes to look one way for one state, and another way for another. But I don't want to have to hardcode all the names
i.e.
TextBox1.BorderStyle = Inset
TextBox2.BorderStyle = Inset
etc.

What I want to do is just pass the page object to ONE sub and have it do the rest.

Just wondering if someone knows of a built in way to get the type of objects on a page other than having to manually cycle through them all.

Thanks

D'Arcy
 
dim c as control
for each c in page.controls
if c.getType.toString() = "System.Web.UI.WebControls.TextBox" then
c.property = value
end if
next

should fix you up.
penny1.gif
penny1.gif
 
Nope, that doesn't work for me.

If you try and go through the page collection, you only get 3 controls: a literal, a htmlform, and another literal.

Thats why the FindControl() works: it actually goes through all the control heirarchies to find it.

here's what I ended up doing:

Public Sub TextBoxLook(ByVal boolValue As Boolean, ByRef objControl As System.Web.UI.Control)
Dim objTextControl As New System.Web.UI.WebControls.TextBox()
Dim i As Integer

Try


If objControl.HasControls Then
For i = 0 To objControl.Controls.Count - 1
TextBoxLook(boolValue, objControl.Controls(i))
Next
Else
If objControl.GetType Is objTextControl.GetType Then

objTextControl = objControl

If boolValue = True Then
objTextControl.BorderStyle = BorderStyle.Inset
objTextControl.BackColor = Color.White
Else
objTextControl.BorderStyle = BorderStyle.None
objTextControl.BackColor = Color.Gainsboro


End If

End If
End If

Catch err As Exception

Finally
objTextControl = Nothing
objControl = Nothing

End Try
End Sub

D'Arcy
 
Good stuff.

I was unaware that you had to drill down through the hierarchy.
penny1.gif
penny1.gif
 
yeah, its a little tricky the way they have the control collections set up.

btw, thanks for the star
:)

D'Arcy
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top