Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles / web / ASP.NET

Disable Child Controls Recursively

5.00/5 (2 votes)
13 Nov 2010CPOL 6.2K  
public void ControlStatus(Control control, bool isDisable){ foreach (Control c in control.Controls) if (c.HasControls()) ControlStatus(c, isDisable); else { WebControl wc = c as WebControl; if (wc != null) ...
C#
public void ControlStatus(Control control, bool isDisable)
{
    foreach (Control c in control.Controls)
        if (c.HasControls())
            ControlStatus(c, isDisable);
        else
        {
            WebControl wc = c as WebControl;

            if (wc != null)
                wc.Enabled = !isDisable;
        }
}


Ignoring exceptions is bad programming practice.
Use 'as' to avoid casting c twice.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)