C#代码总结02---使用泛型来获取Asp前台页面全部控件,并进行属性修改
生活随笔
收集整理的這篇文章主要介紹了
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
總結
以上是生活随笔為你收集整理的C#代码总结02---使用泛型来获取Asp前台页面全部控件,并进行属性修改的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: PAT 1007 Maximum Sub
- 下一篇: c# char unsigned_dll