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
            }

猜你喜欢

转载自www.cnblogs.com/JesseP/p/10681141.html