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;
}
9 个解决方案
#1
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
#2
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).
我知道这是一个老帖子,但这就是我刚刚解决了这个问题的方法。根据标题“如何禁用ASP.NET页面中的所有控件?”我使用Reflection来实现这个目标;它适用于具有Enabled属性的所有控件类型。只需调用在父控件中传递的DisableControls(即表单)。
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
It would be easiest if you put all the controls you want to disable in a panel and then just enable/disable the panel.
如果您在面板中放置要禁用的所有控件,然后启用/禁用面板,这将是最简单的。
#4
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:
在Page_Load里面:
If Not Me.Page.IsPostBack Then
Me.pnlPage.Enabled = False
End If
... or the C# equivalent. :o)
......或者C#等价物。 :O)
#5
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;
}
}
}
#6
I was working with ASP.Net and HTML controls I did like this
我正在使用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);
}
}
called like this
像这样叫
DisableForm(Page.Controls);
#7
Here's a VB.NET version which also takes an optional parameter so it can be used for enabling the controls as well.
这是一个VB.NET版本,它也带有一个可选参数,因此它也可用于启用控件。
Private Sub SetControls(ByVal parentControl As Control, Optional ByVal enable As Boolean = False)
Private Sub SetControls(ByVal parentControl As Control,可选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
#8
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.
我使用linq aproach。使用devExpress时,您必须包含DevExpress.Web.ASPxEditors lib。
#9
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.
如果您确实要禁用页面上的所有控件,那么最简单的方法是将表单的Disabled属性设置为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.
但当然,这也会禁用您的复选框,因此您将无法单击该复选框以重新启用控件。
#1
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
#2
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).
我知道这是一个老帖子,但这就是我刚刚解决了这个问题的方法。根据标题“如何禁用ASP.NET页面中的所有控件?”我使用Reflection来实现这个目标;它适用于具有Enabled属性的所有控件类型。只需调用在父控件中传递的DisableControls(即表单)。
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
It would be easiest if you put all the controls you want to disable in a panel and then just enable/disable the panel.
如果您在面板中放置要禁用的所有控件,然后启用/禁用面板,这将是最简单的。
#4
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:
在Page_Load里面:
If Not Me.Page.IsPostBack Then
Me.pnlPage.Enabled = False
End If
... or the C# equivalent. :o)
......或者C#等价物。 :O)
#5
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;
}
}
}
#6
I was working with ASP.Net and HTML controls I did like this
我正在使用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);
}
}
called like this
像这样叫
DisableForm(Page.Controls);
#7
Here's a VB.NET version which also takes an optional parameter so it can be used for enabling the controls as well.
这是一个VB.NET版本,它也带有一个可选参数,因此它也可用于启用控件。
Private Sub SetControls(ByVal parentControl As Control, Optional ByVal enable As Boolean = False)
Private Sub SetControls(ByVal parentControl As Control,可选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
#8
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.
我使用linq aproach。使用devExpress时,您必须包含DevExpress.Web.ASPxEditors lib。
#9
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.
如果您确实要禁用页面上的所有控件,那么最简单的方法是将表单的Disabled属性设置为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.
但当然,这也会禁用您的复选框,因此您将无法单击该复选框以重新启用控件。