Asp.Net 如何获取所有控件&如何获取指定类型的所有控件


一、

Asp.Net Page页面中访问所有控件的属性为:

Page.Controls

控件的结构是树结构。

二、获取指定类型所有控件实例:

1.递归方法定义:

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

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

2.使用调用:

    List<Literal> allControls = new List<Literal>();
    GetControlList<Literal>(Page.Controls, allControls);
    foreach (var childControl in allControls)
    {
        //call for all controls of the page
    }

 


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2026 CODEPRJ.COM