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

Iterate through controls on a Web form 1

Status
Not open for further replies.

TealWren

Programmer
Jun 7, 2001
231
US
In either C# or VB, I would like to iterate through the controls on my web form and set properties.

I have been unable to find a collection of any sort in the Page class which holds all of the controls on the form. Is there a way to get that information? Has anyone done this?

Thanks for your help!
TealWren
 
Hey Teal,

You'd THINK that the page class would have it, but alas no. Microsoft got sneaky on us.

If you want to go through the controls on your page, you have to cycle through your page's HTMLFORM control.

Your page automatically will have a Controls collection that you an access. What I did was find the HTMLForm control first by doing:

Dim Con as Control

For Each Con In Controls

If Con.GetType Is GetType(HtmlForm) Then

Dim Con2 as Control
For Each Con2 in Con
'code to trigger for each control found
Next

End If

Next


That will allow you to cycle through all the controls within your form tag. Don't worry about having multiple forms either, you can only have one per page anyway, so it'll always find one.

Hope this helped,

Jack
 
Thanks - that works GREAT! I would have /never/ figured that out - I've already been searching the help and the internet since yesterday.

If anyone's looking for the C# equivalent, here it is:

HtmlForm html;
foreach (Control con in Controls)
{
if (con is HtmlForm)
{
html = con as HtmlForm;
foreach (Control con2 in html.Controls)
{
//code for each control found
}
}
}

Thanks again! TealWren
 
I just did a copy & paste of the VB code, but I'm getting an error on the "Con" in this line: For Each Con2 in Con. The error states, "Expression is of type 'System.Web.UI.Control', which is not a collection type. Any ideas?
 
You are really looking for each con2 in the html controls collection, so it should be something more like:

Dim Con as Control
Dim html as HtmlForm

For Each Con In Controls

If Con.GetType Is GetType(HtmlForm) Then

Dim Con2 as Control
html = con as HtmlForm
For Each Con2 in html.Controls
'code to trigger for each control found
Next

End If

Next


That's just a guess though - I'm not positive on the VB syntax.

Good luck!

TealWren
 
Rats! I can't get that to work either! The errors I get point to the html = con as HtmlForm line. "Option Strict On disallows implicit conversion from 'System.Web.UI.Control' to 'System.Web.UI.HtmlControls.HtmlForm'. The 2nd error I get is: "End of statement expected."

Are we really looking through the HTML controls collection since these are System.Web.UI.Controls within the HtmlForm? I've tried a bunch of things; I just can't get it to work! It doesn't help that my "Watch Window" has frozen up now!
 
Hey Linda,

Teal included one bit of code in his c# example that i neglected to add in mine. The code should look like this:

Dim Con as Control

For Each Con In Controls

If Con.GetType Is GetType(HtmlForm) Then

Dim Con2 as Control
For Each Con2 in Con.Controls
'code to trigger for each control found
Next

End If

Next

Although the htmlform is a control itself, it has a controls collection that will contain all the other controls within it.

Jack
 
Thanks, that helped. When I only found five "Con2" controls, I panicked; but two of those controls were panels, which I found contain their own controls collection, so I had to go down another level to get at the actual controls that I need to manipulate.

That brings up a new issue! I need to change the forecolor property on all the label controls within each panel. Intellisense doesn't give me any 'obvious' option to set the forecolor property! Do you have any ideas?

My debugger watch window shows the following hierarchy under Con3:

-Con3
- System.Web.UI.WebControls.Label
- System.Web.UI.WebControls.Webcontrol
+ Forecolor


My code thus far:

Dim Con as Control
For Each Con In Controls
If Con.GetType Is GetType(HtmlForm) Then
Dim Con2 as Control
For Each Con2 in Con.Controls
If Con2.GetType Is GetType(Panel) Then
Dim Con3 as Control
For each Con3 in Con2.Controls
If Con3.GetType Is GetType(Label) Then
'need to set Forecolor property to red
End IF
Next
End If
Next
End If
Next
 
Hey Linda,

accessing controls this way won't bring up intellisense. its not "intelligent" enough

Ha ha ha, get it?!

heh, ok, so anyway...

even though you don't see the intellisense, you can still set the forecolor property (or text property, or whatever property), you just need to do it from memory. It's the same thing when you assign something that has accessible properties to a Session variable: you can write code to access those properties, but intellisense won't show up.

Hope this helped
:)

jack
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top