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

master page question

Status
Not open for further replies.

fadetoblack

Programmer
Jul 19, 2005
19
US
I know that I can cycle through the controls of a standard .aspx form using the following:

foreach (Control c in Page.Form.Controls) {

}

What do I use to find the controls of user controls on a master page?

 
just check the type as you loop to see if it is a usercontrol.. check typeOF.
 
The code below doesn't capture the usercontrols yet. What am I missing?

foreach (Control c in Page.Form.Controls) {
if(typeof(UserControl)==c.GetType()){

}
}
 
Code:
        foreach (Control c in Page.Form.Controls)
        {
            if (typeof(UserControl).Equals(c.GetType()))
            {

            }
        }
 
The if statement still isn't capturing anything

foreach (Control c in Page.Form.Controls) {
if(typeof(UserControl).Equals(c.GetType())){

}
}
 
You need to recursively loop through the objects..
I don't know C# but I converted some VB code I have used..
See if this works for you ..

Code:
    void CheckObjects(Control parent) {
        Control c;
        foreach (c in Parent.Controls) {
            if ((c.GetType() == UserControl)) {
                //DoSomethingHere;
            }
            if ((c.Controls.Count > 0)) {
                CheckObjects(c);
            }
        }
    }
 
My test was:

foreach (Control c in Page.Form.Controls) {
if(typeof(Button).Equals(c.GetType())){
Response.Write(c.ID());
}
}

I was checking for Buttons. It worked.
Why would you want to access directly the controls in a custom User Control? I am not sure but i think that you must declare the control in the UC as public. I would expose methods instead of accessing them (functions, get/set, etc)

 
Why I would like to access the uc controls is an attempt to bypass the Event Bubbling concept. I don't understand it well enough to use it and haven't found ANY good examples. I ran across this example for EDP @ using local controls and am trying to adapt it to access the uc controls to assign click handling.
 
jbenson001: I had to change what you gave me a little to get it past the debugger but still no luck on locating user controls.

private void CheckObjects(Control parent) {
foreach (Control c in parent.Controls) {
if (c.GetType() == typeof(UserControl)) {
string test = c.ID;
}
if (c.Controls.Count > 0) CheckObjects(c);
}
}
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top