21

I have multiple dropdownlist in a page and would like to disable all if user selects a checkbox which reads disable all. So far I have this code and it is not working. Any suggestions?

foreach (Control c in this.Page.Controls)
{
    if (c is DropDownList)
        ((DropDownList)(c)).Enabled = false;
}
Jim G.
  • 14,056
  • 19
  • 94
  • 153
Mohamed
  • 3,092
  • 7
  • 25
  • 31

9 Answers9

39

Each control has child controls, so you'd need to use recursion to reach them all:

protected void DisableControls(Control parent, bool State) {
    foreach(Control c in parent.Controls) {
        if (c is DropDownList) {
            ((DropDownList)(c)).Enabled = State;
        }

        DisableControls(c, State);
    }
}

Then call it like so:

protected void Event_Name(...) {
    DisableControls(Page,false); // use whatever top-most control has all the dropdowns or just the page control
} // divs, tables etc. can be called through adding runat="server" property
bobble14988
  • 1,694
  • 4
  • 25
  • 36
John Sheehan
  • 74,152
  • 28
  • 154
  • 191
  • Thanks! I wanted to turn off all the buttons/textboxes/comboboxes on a form except for one, and disabling a panel disables all the controls so it won't work. Using your method, I could turn off just the controls I wanted, but not the panels. – ajs410 Mar 17 '10 at 14:50
32

I know this is an old post but this is how I have just solved this problem. AS per the title "How do I disable all controls in ASP.NET page?" I used Reflection to achieve this; it will work on all control types which have an Enabled property. Simply call DisableControls passing in the parent control (I.e., Form).

C#:

private void DisableControls(System.Web.UI.Control control)
{
    foreach (System.Web.UI.Control c in control.Controls) 
    {
        // Get the Enabled property by reflection.
        Type type = c.GetType();
        PropertyInfo prop = type.GetProperty("Enabled");

        // Set it to False to disable the control.
        if (prop != null) 
        {
            prop.SetValue(c, false, null);
        }

        // Recurse into child controls.
        if (c.Controls.Count > 0) 
        {
            this.DisableControls(c);
        }
    }
}

VB:

    Private Sub DisableControls(control As System.Web.UI.Control)

        For Each c As System.Web.UI.Control In control.Controls

            ' Get the Enabled property by reflection.
            Dim type As Type = c.GetType
            Dim prop As PropertyInfo = type.GetProperty("Enabled")

            ' Set it to False to disable the control.
            If Not prop Is Nothing Then
                prop.SetValue(c, False, Nothing)
            End If

            ' Recurse into child controls.
            If c.Controls.Count > 0 Then
                Me.DisableControls(c)
            End If

        Next

    End Sub
Richard Pursehouse
  • 1,049
  • 11
  • 20
Justin Clarke
  • 562
  • 6
  • 7
  • 3
    This is the best solution because it disables any control that has the "Enabled" property. My current project can have a variety of custom controls inside a certain fieldset, it was ugly to only disable certain known types. – ASalazar Apr 15 '13 at 17:55
  • 1
    Perfect for me! Thanks. – MAW74656 Oct 31 '13 at 16:15
19

It would be easiest if you put all the controls you want to disable in a panel and then just enable/disable the panel.

bechbd
  • 5,190
  • 3
  • 24
  • 46
9

Put a panel around the part of the page that you want disabled:

   < asp:Panel ID="pnlPage" runat="server" >
      ...
   < /asp:Panel >

Inside of Page_Load:

   If Not Me.Page.IsPostBack Then
      Me.pnlPage.Enabled = False
   End If

... or the C# equivalent. :o)

Jack
  • 10,783
  • 12
  • 46
  • 65
Tom English
  • 91
  • 1
  • 1
2

I was working with ASP.Net and HTML controls I did like this

public void DisableForm(ControlCollection ctrls)
    {
        foreach (Control ctrl in ctrls)
        {
            if (ctrl is TextBox)
                ((TextBox)ctrl).Enabled = false;
            if (ctrl is Button)
                ((Button)ctrl).Enabled = false;
            else if (ctrl is DropDownList)
                ((DropDownList)ctrl).Enabled = false;
            else if (ctrl is CheckBox)
                ((CheckBox)ctrl).Enabled = false;
            else if (ctrl is RadioButton)
                ((RadioButton)ctrl).Enabled = false;
            else if (ctrl is HtmlInputButton)
                ((HtmlInputButton)ctrl).Disabled = true;
            else if (ctrl is HtmlInputText)
                ((HtmlInputText)ctrl).Disabled = true;
            else if (ctrl is HtmlSelect)
                ((HtmlSelect)ctrl).Disabled = true;
            else if (ctrl is HtmlInputCheckBox)
                ((HtmlInputCheckBox)ctrl).Disabled = true;
            else if (ctrl is HtmlInputRadioButton)
                ((HtmlInputRadioButton)ctrl).Disabled = true;

            DisableForm(ctrl.Controls);
        }
    }

called like this

DisableForm(Page.Controls);
dnxit
  • 6,192
  • 2
  • 25
  • 32
1
  private void ControlStateSwitch(bool state)
{
    foreach (var x in from Control c in Page.Controls from Control x in c.Controls select x)
        if (ctrl is ASPxTextBox)

            ((ASPxTextBox)x).Enabled = status;

        else if (x is ASPxDateEdit)

            ((ASPxDateEdit)x).Enabled = status;
}

I use a linq aproach. While using devExpress you must include DevExpress.Web.ASPxEditors lib.

Coderx07
  • 178
  • 6
1

You have to do this recursive, I mean you have to disable child controls of controls to :

protected void Page_Load(object sender, EventArgs e)
{
  DisableChilds(this.Page);
}

private void DisableChilds(Control ctrl)
{
   foreach (Control c in ctrl.Controls)
   {
      DisableChilds(c);
      if (c is DropDownList)
      {
           ((DropDownList)(c)).Enabled = false;
      }
    }
}
Canavar
  • 46,286
  • 17
  • 83
  • 120
1

Here's a VB.NET version which also takes an optional parameter so it can be used for enabling the controls as well.

Private Sub SetControls(ByVal parentControl As Control, Optional ByVal enable As Boolean = False)

    For Each c As Control In parentControl.Controls
        If TypeOf (c) Is CheckBox Then
            CType(c, CheckBox).Enabled = enable
        ElseIf TypeOf (c) Is RadioButtonList Then
            CType(c, RadioButtonList).Enabled = enable
        End If
        SetControls(c)
    Next

End Sub
0

If you really want to disable all controls on a page, then the easiest way to do this is to set the form's Disabled property to true.

ASPX:

<body>
    <form id="form1" runat="server">
      ...
    </form>
</body>

Code-behind:

protected void Page_Load(object sender, EventArgs e)
{
    form1.Disabled = true;
}

But of course, this will also disable your checkbox, so you won't be able to click the checkbox to re-enable the controls.

M4N
  • 90,223
  • 44
  • 210
  • 255