如何在ASP.NET页面中禁用所有控件?

23

我在网页中有多个下拉列表,如果用户选择一个名为“禁用所有”的复选框,则希望禁用所有下拉列表。到目前为止,我已经编写了以下代码,但它没有起作用。请问有什么建议吗?

foreach (Control c in this.Page.Controls)
{
    if (c is DropDownList)
        ((DropDownList)(c)).Enabled = false;
}
9个回答

40
每个控件都有子控件,因此您需要使用递归来访问它们所有的控件:
protected void DisableControls(Control parent, bool State) {
    foreach(Control c in parent.Controls) {
        if (c is DropDownList) {
            ((DropDownList)(c)).Enabled = State;
        }

        DisableControls(c, State);
    }
}

那么就这样调用它:

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

谢谢!我想关闭表单上除一个之外的所有按钮/文本框/组合框,但禁用面板会禁用所有控件,所以这不起作用。使用您的方法,我可以关闭我想要的控件,但不能关闭面板。 - ajs410

34

我知道这是一个旧帖子,但这就是我刚刚解决这个问题的方法。根据标题“如何禁用ASP.NET页面中的所有控件?”,我使用了反射来实现这一点;它将适用于所有具有Enabled属性的控件类型。只需调用DisableControls并传入父控件(即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

3
这是最佳解决方案,因为它禁用了具有“启用”属性的任何控件。我的当前项目可以在某个字段集合内包含各种自定义控件,只禁用某些已知类型的控件会很丑陋。 - ASalazar
1
非常适合我!谢谢。 - MAW74656
如果控件有多个子控件,它会抛出异常。 - Hiren Patel

20

如果您要禁用多个控件,最好将它们全部放在一个面板中,然后只需启用/禁用该面板即可。


10

在页面上需要禁用的区域周围放置面板:

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

在Page_Load中:

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

...或者C#等效语言。 :o)


2
您需要递归执行此操作,也就是说,您需要禁用控件的子控件:
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;
      }
    }
}

2

我曾经使用ASP.Net和HTML控件,我的写法如下:

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

被称为这样
DisableForm(Page.Controls);

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

我使用linq方法。在使用devExpress时,您必须包含DevExpress.Web.ASPxEditors库。


1
这是一个VB.NET版本,它还带有一个可选参数,因此它也可以用于启用控件。
私有子程序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
如果您真的想要禁用页面上所有控件,那么最简单的方法是将表单的Disabled属性设置为true。
ASPX:
<body>
    <form id="form1" runat="server">
      ...
    </form>
</body>

代码后台:

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

当然,这也会禁用您的复选框,因此您将无法单击复选框以重新启用控件。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接