Click here to Skip to main content
15,881,172 members
Articles / Web Development / ASP.NET
Tip/Trick

Disable Child Controls Recursively

Rate me:
Please Sign up or sign in to vote.
4.00/5 (5 votes)
31 Oct 2010CPOL 21K   7   2

Trying to disable a div on a certain event, but for a div there is no such a property named Enabled, and if we use attribute["disabled"] the controls inside are not disabled, just have a gray color.. !! Same when using a UserControl that have child controls


The child controls will not be disabled..


So, I wrote a method that uses recursion to go down deep and disable each child Control in any given parent Control.


The method takes two parameters



  • 1st the main control
  • 2nd status flage

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

License

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


Written By
Software Developer
Egypt Egypt
- BSc Computer Engineering
Ain Shams University - Faculty of Engineering

Comments and Discussions

 
GeneralMy vote of 2 Pin
Member 1080762812-Feb-15 1:36
Member 1080762812-Feb-15 1:36 
GeneralReason for my vote of 4 Nice Tip Pin
TweakBird28-Oct-10 23:31
TweakBird28-Oct-10 23:31 

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.