C#代码总结02---使用泛型来获取Asp前台页面全部控件,并进行属性修改

该方法:主要用于对前台页面的不同类型(TextBox、DropDownList、等)或全部控件进行批量操作,用于批量修改其属性(如,Text、Enable)。

 

private void GetControlList<T>(ControlCollection controlCollection, List<T> resultCollection)
    where T : Control
    {
        foreach (Control control in controlCollection)
        {     
                if (control is T) 
                resultCollection.Add((T)control);

            if (control.HasControls())
                GetControlList(control.Controls, resultCollection);
        }
    }

 

调用 ✔  :主要是用来禁用 Enable 属性,将其变为不可用。

            //隐藏页面关于资产分类的选择控件,以及保存和提交按钮。

            List<TextBox> allTextBoxs = new List<TextBox>();  //对Textbox进行禁用
            GetControlList<TextBox>(Page.Controls, allTextBoxs);
            foreach (var childControl in allTextBoxs)
            {
                childControl.Enabled = false; //call for all controls of the page
            }

            List<DropDownList> allDDLs = new List<DropDownList>();  //对dropdownlist进行禁用
            GetControlList<DropDownList>(Page.Controls, allDDLs);
            foreach (var childControl in allDDLs)
            {
                childControl.Enabled = false; //call for all controls of the page
            }

 

转载于:https://www.cnblogs.com/JesseP/p/10681141.html

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值