ASP.NET C# - Disable All Controls on a form

I have the below code.  I am trying to disable the controls on my form.  This is not working.  I tried to follow the example here:

http://www.codeproject.com/Articles/19699/Enable-or-disable-all-controls-in-the-page-using-s

but no luck.  How can I disable the controls on my form?  Also, how can I get the name of the control while looping through it (I will need to put code in to NOT disable certain controls)?

    protected void Page_PreRender(object sender, EventArgs e)
    {
        bool status = false;
        foreach (Control c in Page.Controls)
            foreach (Control ctrl in c.Controls)
                if (ctrl is TextBox)
                    ((TextBox)ctrl).Enabled = status;
                else if (ctrl is Button)
                    ((Button)ctrl).Enabled = status;
                else if (ctrl is RadioButton)
                    ((RadioButton)ctrl).Enabled = status;
                else if (ctrl is ImageButton)
                    ((ImageButton)ctrl).Enabled = status;
                else if (ctrl is CheckBox)
                    ((CheckBox)ctrl).Enabled = status;
                else if (ctrl is DropDownList)
                    ((DropDownList)ctrl).Enabled = status; 
                else if (ctrl is HyperLink)
                    ((HyperLink)ctrl).Enabled = status;
                else if (ctrl is LinkButton)
                    ((LinkButton)ctrl).Enabled = status; 
    }

Open in new window

LVL 2
CipherISAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Kyle AbrahamsSenior .Net DeveloperCommented:
You need to recurse it:

Call this from your prerender:

DisableControls (Control c, bool status)
{
       foreach (Control ctrl in c.Controls)
       {
                if (ctrl is TextBox)
                    ((TextBox)ctrl).Enabled = status;
                else if (ctrl is Button)
                    ((Button)ctrl).Enabled = status;
                else if (ctrl is RadioButton)
                    ((RadioButton)ctrl).Enabled = status;
                else if (ctrl is ImageButton)
                    ((ImageButton)ctrl).Enabled = status;
                else if (ctrl is CheckBox)
                    ((CheckBox)ctrl).Enabled = status;
                else if (ctrl is DropDownList)
                    ((DropDownList)ctrl).Enabled = status;
                else if (ctrl is HyperLink)
                    ((HyperLink)ctrl).Enabled = status;
                else if (ctrl is LinkButton)
                    ((LinkButton)ctrl).Enabled = status;

                 if (ctrl.HasControls)
                      DisableControls(ctrl);
        }
  // use ctrl.ID to check the name.
   
}
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
CipherISAuthor Commented:
I figured out why it wasn't working.  Thx for giving me the ID

 foreach (Control c in Form.Controls)  //THIS WAS THE CHANGE
        {
            foreach (Control ctrl in c.Controls)
            {
                if (ctrl.ID != null && ctrl.ID.Contains("txtPlace"))
                    break;

                if (ctrl is TextBox)
                    ((TextBox)ctrl).Enabled = status;
                else if (ctrl is Button)
                    ((Button)ctrl).Enabled = status;
                else if (ctrl is RadioButton)

Open in new window

0
Kyle AbrahamsSenior .Net DeveloperCommented:
That won't work for more complicated layouts.

Consider:

<div>
  <asp:panel>
     <asp:updatepanel>
         <grid>  --> won't be found.  

The only way to properly do that is to recurse.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
ASP.NET

From novice to tech pro — start learning today.