public static class ControlHelper
{
#region 同时禁用或者启用页面的某些控件
/// <summary>
/// 设置是否启用控件
/// </summary>
/// <param name="control"></param>
/// <param name="controlName"></param>
/// <param name="isEnable"></param>
public static void SetControlsEnabled(Control control, ControlNameEnum controlName, bool isEnabled)
{
foreach (Control item in control.Controls)
{
/* 我们仅仅考虑几种常用的asp.net服务器控件和html控件 */
//Panel
if (item is Panel && (controlName == ControlNameEnum.Panel || controlName == ControlNameEnum.All))
{
((Panel)item).Enabled = isEnabled;
}
//TextBox,HtmlTextBox
if (controlName == ControlNameEnum.TextBox || controlName == ControlNameEnum.All)
{
if (item is TextBox)
{
((TextBox)(item)).Enabled = isEnabled;
}
else if (item is HtmlInputText)
{
((HtmlInputText)item).Disabled = isEnabled;
}
else if (item is HtmlTextArea)
{
((HtmlTextArea)(item)).Disabled = isEnabled;
}
}
//Buttons
if (item is Button && (controlName == ControlNameEnum.Button || controlName == ControlNameEnum.All))
{
if (item is Button)
{
((Button)(item)).Enabled = isEnabled;
}
else if (item is HtmlInputButton)
{
((HtmlInputButton)(item)).Disabled = !isEnabled;
}
else if (item is ImageButton)
{
((ImageButton)(item)).Enabled = isEnabled;
}
else if (item is LinkButton)
{
((LinkButton)(item)).Enabled = isEnabled;
}
}
//CheckBox
if (controlName == ControlNameEnum.CheckBox || controlName == ControlNameEnum.All)
{
if (item is CheckBox)
{
((CheckBox)(item)).Enabled = isEnabled;
}
else if (item is HtmlInputCheckBox)
{
((HtmlInputCheckBox)(item)).Disabled = !isEnabled;
}
}
//List Controls
if (controlName == ControlNameEnum.ListControl || controlName == ControlNameEnum.All)
{
if (item is DropDownList)
{
((DropDownList)(item)).Enabled = isEnabled;
}
else if (item is RadioButtonList)
{
((RadioButtonList)(item)).Enabled = isEnabled;
}
else if (item is CheckBoxList)
{
((CheckBoxList)(item)).Enabled = isEnabled;
}
else if (item is ListBox)
{
((ListBox)(item)).Enabled = isEnabled;
}
else if (item is HtmlSelect)
{
((HtmlSelect)(item)).Disabled = !isEnabled;
}
}
//如果项目还有子控件,递归调用该函数
if (item.Controls.Count > 0)
{
SetControlsEnabled(item, controlName, isEnabled);
}
}
}
}
#endregion
ControlHelper.SetControlsEnabled(this.Page, ControlNameEnum.Panel, false); //Panel禁用
ControlHelper.SetControlsEnabled(this.Page, ControlNameEnum.TextBox, false); //TextBox禁用
ControlHelper.SetControlsEnabled(this.Page, ControlNameEnum.ListControl, false); //TextBox禁用
ControlHelper.SetControlsEnabled(this.Page, ControlNameEnum.All, false); //TextBox禁用
另一种
private void FindControls(ControlCollection cc)
{
foreach (Control c in cc)
{
if (c.HasControls())
FindControls(c.Controls);
else
{
if (c is System.Web.UI.WebControls.TextBox)
(c as System.Web.UI.WebControls.TextBox).Enabled = false;
if (c is DropDownList)
(c as System.Web.UI.WebControls.DropDownList).Enabled = false;
if (c is System.Web.UI.WebControls.Label)
(c as System.Web.UI.WebControls.Label).Enabled = false;
if (c is System.Web.UI.HtmlControls.HtmlForm)
(c as System.Web.UI.HtmlControls.HtmlForm).Disabled = true;
if (c is System.Web.UI.HtmlControls.HtmlInputText)
(c as System.Web.UI.HtmlControls.HtmlInputText).Disabled = true;
}
}
}
FindControls(this.form1.Controls);