65.9K
CodeProject is changing. Read more.
Home

Disable Child Controls Recursively

starIconstarIconstarIconstarIconstarIcon

5.00/5 (2 votes)

Nov 2, 2010

CPOL
viewsIcon

6212

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) ...

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.