Click here to Skip to main content
16,016,301 members
Articles / Web Development / ASP.NET
Alternative
Tip/Trick

Disable Child Controls Recursively

Rate me:
Please Sign up or sign in to vote.
5.00/5 (1 vote)
13 Nov 2010CPOL 10.2K   2   1
You can just use "not IsDisable" versus the IF...ELSE block. Additionally, the recursive call to ControlStatus should be done all the time so that the parent control is enabled/disabled along with the children. // to enable\disable the control & its child controls public...
You can just use "not IsDisable" versus the IF...ELSE block. Additionally, the recursive call to ControlStatus should be done all the time so that the parent control is enabled/disabled along with the children.

// to enable\disable the control & its child controls
public void ControlStatus(Control control, bool isDisable)
{
    foreach (Control c in control.Controls)
        try
        {
            if (c is WebControl)
                ((WebControl)c).Enabled = !isDisable;

            if (c.HasChildren)
                ControlStatus(c, isDisable);
        }
        catch (Exception)
        { }
}

License

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


Written By
Technical Lead Self-employed, consultant to US Air Force
United States United States
Enterprise architect/senior developer/SME for electronic warfare software development at the US Air Force 350th Spectrum Warfare Wing, Eglin AFB, FL.

Comments and Discussions

 
GeneralI like the "not IsDisable" part :), but why do we need the p... Pin
AntounPG8-Nov-10 0:43
AntounPG8-Nov-10 0:43 
I like the "not IsDisable" part Smile | :) ,
but why do we need the parent control to be enabled/disabled along with the children ?

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.