码迷,mamicode.com
首页 > Web开发 > 详细

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

时间:2018-05-24 19:34:56      阅读:236      评论:0      收藏:0      [点我收藏+]

标签:asp.net   clean   call   使用   asc   oid   for   访问   list   

一、

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
    }

 

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

标签:asp.net   clean   call   使用   asc   oid   for   访问   list   

原文地址:https://www.cnblogs.com/lgx5/p/9084539.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!