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

问题描述:

我在页面中有多个下拉列表,并且想要禁用所有用户选择禁用全部的复选框。到目前为止,我有这个代码,它不工作。有什么建议么?如何禁用ASP.NET页面中的所有控件?

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

每个控件都有子控件,所以你需要使用递归来达到他们都:

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 
+0

谢谢!我想关闭除了一个窗体以外的窗体上的所有按钮/文本框/组合框,并且禁用面板会禁用所有控件,因此它不起作用。使用你的方法,我可以关闭我想要的控件,但不是面板。 – ajs410 2010-03-17 14:50:07

如果你想在面板中禁用所有的控件,然后启用/禁用面板,这将是最简单的。

你要做这个递归,我的意思是你必须禁用子控件的控件:

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

这里是一个VB.NET版本,它也需要一个选择离子参数,因此它也可以用于启用控制。

私人小组SetControls(BYVAL parentControl作为对照,可选BYVAL使由于布尔= 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 

如果你真的想在页面上禁用所有控制,那么最简单的方式做这是将窗体的Disabled属性设置为true。

ASPX:

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

代码隐藏:

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

不过,当然,这也将禁用复选框,这样你就无法点击复选框以重新启用控制。

放在面板周围要禁用页面的一部分:

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

里面的Page_Load中:

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

...或C#等效。 :o)

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 aproach。在使用devExpress时,您必须包含 DevExpress.Web.ASPxEditors库。

我知道这是一个旧的帖子,但这是我刚刚解决这个问题。按照标题“如何禁用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 
+2

这是最好的解决方案,因为它会禁用具有“已启用”属性的任何控件。我当前的项目可以在特定的字段集中包含各种自定义控件,仅禁用某些已知类型就很难看。 – ASalazar 2013-04-15 17:55:27

我和我做这样的

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); 
ASP.Net和HTML控件工作