zoukankan      html  css  js  c++  java
  • 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
        }
  • 相关阅读:
    简单工厂模式
    原型模式
    特性Attribute
    MVC_Route层层深入
    异步Async
    sql-connectionStrings
    观察者模式(利用委托)
    SqlServer_存储过程
    c语言----程序记录
    c语言基础笔记
  • 原文地址:https://www.cnblogs.com/lgx5/p/9084539.html
Copyright © 2011-2022 走看看