publiclass="tags" href="/tags/C.html" title=c>c staticlass="tags" href="/tags/C.html" title=c>c class="tags" href="/tags/C.html" title=c>class ControlHelper
{
#region 同时禁用或者启用页面的某些控件
/// <summary>
/// 设置是否启用控件
/// </summary>
/// <param name="class="tags" href="/tags/C.html" title=c>control"></param>
/// <param name="class="tags" href="/tags/C.html" title=c>controlName"></param>
/// <param name="isEnable"></param>
publiclass="tags" href="/tags/C.html" title=c>c staticlass="tags" href="/tags/C.html" title=c>c void SetControlsEnabled(Control class="tags" href="/tags/C.html" title=c>control, ControlNameEnum class="tags" href="/tags/C.html" title=c>controlName, bool isEnabled)
{
foreaclass="tags" href="/tags/C.html" title=c>ch (Control item in class="tags" href="/tags/C.html" title=c>control.Controls)
{
/* 我们仅仅考虑几种常用的class="tags" href="/tags/C.html" title=c>class="tags" href="/tags/ASP.NET.html" title=asp.net>asp.netclass="tags" href="/tags/FuWuQi.html" title=服务器>服务器控件和html控件 */
//Panel
if (item is Panel && (class="tags" href="/tags/C.html" title=c>controlName == ControlNameEnum.Panel || class="tags" href="/tags/C.html" title=c>controlName == ControlNameEnum.All))
{
((Panel)item).Enabled = isEnabled;
}
//TextBox,HtmlTextBox
if (class="tags" href="/tags/C.html" title=c>controlName == ControlNameEnum.TextBox || class="tags" href="/tags/C.html" title=c>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 && (class="tags" href="/tags/C.html" title=c>controlName == ControlNameEnum.Button || class="tags" href="/tags/C.html" title=c>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;
}
}
//Checlass="tags" href="/tags/C.html" title=c>ckBox
if (class="tags" href="/tags/C.html" title=c>controlName == ControlNameEnum.Checlass="tags" href="/tags/C.html" title=c>ckBox || class="tags" href="/tags/C.html" title=c>controlName == ControlNameEnum.All)
{
if (item is Checlass="tags" href="/tags/C.html" title=c>ckBox)
{
((Checlass="tags" href="/tags/C.html" title=c>ckBox)(item)).Enabled = isEnabled;
}
else if (item is HtmlInputCheclass="tags" href="/tags/C.html" title=c>ckBox)
{
((HtmlInputCheclass="tags" href="/tags/C.html" title=c>ckBox)(item)).Disabled = !isEnabled;
}
}
//List Controls
if (class="tags" href="/tags/C.html" title=c>controlName == ControlNameEnum.ListControl || class="tags" href="/tags/C.html" title=c>controlName == ControlNameEnum.All)
{
if (item is DropDownList)
{
((DropDownList)(item)).Enabled = isEnabled;
}
else if (item is RadioButtonList)
{
((RadioButtonList)(item)).Enabled = isEnabled;
}
else if (item is Checlass="tags" href="/tags/C.html" title=c>ckBoxList)
{
((Checlass="tags" href="/tags/C.html" title=c>ckBoxList)(item)).Enabled = isEnabled;
}
else if (item is ListBox)
{
((ListBox)(item)).Enabled = isEnabled;
}
else if (item is HtmlSeleclass="tags" href="/tags/C.html" title=c>ct)
{
((HtmlSeleclass="tags" href="/tags/C.html" title=c>ct)(item)).Disabled = !isEnabled;
}
}
//如果项目还有子控件class="tags" href="/tags/C.html" title=c>c;递归调用该函数
if (item.Controls.Count > 0)
{
SetControlsEnabled(item, class="tags" href="/tags/C.html" title=c>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(ControlColleclass="tags" href="/tags/C.html" title=c>ction class="tags" href="/tags/C.html" title=c>cclass="tags" href="/tags/C.html" title=c>c)
{
foreaclass="tags" href="/tags/C.html" title=c>ch (Control class="tags" href="/tags/C.html" title=c>c in class="tags" href="/tags/C.html" title=c>cclass="tags" href="/tags/C.html" title=c>c)
{
if (class="tags" href="/tags/C.html" title=c>c.HasControls())
FindControls(class="tags" href="/tags/C.html" title=c>c.Controls);
else
{
if (class="tags" href="/tags/C.html" title=c>c is System.Web.UI.WebControls.TextBox)
(class="tags" href="/tags/C.html" title=c>c as System.Web.UI.WebControls.TextBox).Enabled = false;
if (class="tags" href="/tags/C.html" title=c>c is DropDownList)
(class="tags" href="/tags/C.html" title=c>c as System.Web.UI.WebControls.DropDownList).Enabled = false;
if (class="tags" href="/tags/C.html" title=c>c is System.Web.UI.WebControls.Label)
(class="tags" href="/tags/C.html" title=c>c as System.Web.UI.WebControls.Label).Enabled = false;
if (class="tags" href="/tags/C.html" title=c>c is System.Web.UI.HtmlControls.HtmlForm)
(class="tags" href="/tags/C.html" title=c>c as System.Web.UI.HtmlControls.HtmlForm).Disabled = true;
if (class="tags" href="/tags/C.html" title=c>c is System.Web.UI.HtmlControls.HtmlInputText)
(class="tags" href="/tags/C.html" title=c>c as System.Web.UI.HtmlControls.HtmlInputText).Disabled = true;
}
}
}
FindControls(this.form1.Controls);