How do I disable all controls in ASP.NET page?
Asked Answered
E

9

23

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;
}
Emmalineemmalyn answered 2/2, 2009 at 23:1 Comment(1)
Check this: codingfusion.com/Post/…Frisket
Z
41

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
Zymo answered 2/2, 2009 at 23:10 Comment(1)
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.Peba
R
35

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
Ragsdale answered 9/4, 2013 at 15:15 Comment(2)
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.Gild
It's throwing an exception if control having multiple child controls.Contributory
B
20

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

Beccafico answered 2/2, 2009 at 23:4 Comment(0)
O
10

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)

Outfox answered 30/10, 2012 at 17:40 Comment(0)
S
2

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;
      }
    }
}
Snowonthemountain answered 2/2, 2009 at 23:15 Comment(0)
S
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);
Seamaid answered 29/8, 2014 at 16:55 Comment(0)
T
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
Taranto answered 10/2, 2009 at 20:15 Comment(0)
M
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.

Murray answered 4/12, 2012 at 11:19 Comment(0)
R
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.

Roche answered 10/2, 2009 at 20:22 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.