Click here to Skip to main content
15,894,362 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 21.1K   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 
This code is just clumsy and promotes bad practicies:
- catch(Exception) catches AND SWALLOWS everything for no reason - do you really want to accidentally catch OutOfMemoryException for example?
- And never, never use empty catch statement - at least do some logging. This will really, really help you later in debugging strange problems
- there is no need to explicitly check for .HasControls() - you can just loop over .Controls as you do and it will do nothing if collection is empty
- isDisable is not very good parameter name, I would use something like "bool disabled", or, even better, reverse the meaning of the parameter and name it "bool enabled"
- if (isDisable) is insane, you could just write ((WebControl)c).Enabled = !isDisable;

I couldn't give you more than 2/5 - sorry for being mean.
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.