Click here to Skip to main content
Click here to Skip to main content

Disable Child Controls Recursively

, 31 Oct 2010
Rate this:
Please Sign up or sign in to vote.

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)

Share

About the Author

Antoun Gorgy
Software Developer
Egypt Egypt
- BSc Computer Engineering
Ain Shams University - Faculty of Engineering

Comments and Discussions

 
GeneralReason for my vote of 4 Nice Tip PinmemberEswa28-Oct-10 23:31 

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

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

| Advertise | Privacy | Mobile
Web04 | 2.8.140827.1 | Last Updated 31 Oct 2010
Article Copyright 2010 by Antoun Gorgy
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid