禁用页面控件

 

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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值