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