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

梦想与她 提交于 2019-11-28 19:24:05
John Sheehan

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
Justin Clarke

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

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

Tom English

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)

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);

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;
      }
    }
}

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
  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.

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.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!