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!

Loop Through Web Controls When Using Master Page

Status
Not open for further replies.

mrgulic

Technical User
Sep 18, 2001
248
US
I am trying to perform control.enable = true/false on a page by looping through all the controls.

I have tried to modify the following method that I found on to suit my needs with no sucess.

Code:
public static Control FindControlRecursive(Control Root, string Id)
{
    if (Root.ID == Id)
        return Root;

    foreach (Control Ctl in Root.Controls)
    {
        Control FoundCtl = FindControlRecursive(Ctl, Id);
        if (FoundCtl != null)
            return FoundCtl;
    }
    return null;
}


Basically all that I want to do is:

Code:
foreach(Control ctl in Page.Controls)
{
     ctl.Enabled = false;
}

problem with the code that i found is

1. I am not calling the method, and if i was i wouldn't know what to set as the "Root" or "ID"
2. Intellisense does not show that I can assign .Enabled to the discovered control.

Thanks in advance for your assistance.
 
the code from west-wind only finds the control, it doesn't do anything with it. the root is where you start from (a master page, a page, a user control) because you may not want/need to look through every control from the very top. the id is the name of the control to find (you need to explicitly know what you are looking for).

Control does not have an Enabled property. You need to determine if the control is a web control. if so cast down and set Enabled.

You could modify the west-wind code to return an enumeration of all webcontrols. but that doesn't really provide value. rather that dive into the the internal structure of all your pags/user controls/masters. use the concept of encapsulation to hide the details of a particular action.

I would ask the question: why disable every control on the page? It doesn't make sense. Maybe you are trying to achieve a level of security by disabling controls, maybe not. In any case there is most likely a better solution to the problem. If you expand on what you're requirement is, then we may be able to point you in a different direction.

Jason Meckley
Programmer
Specialty Bakers, Inc.

faq855-7190
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top