zoukankan      html  css  js  c++  java
  • ASP.Net 控件的遍历(转+修改http://www.cnblogs.com/kerryking/archive/2007/10/19/930120.html)

       页面可以被看成各种控件组成的一个集合。在页面被初始化和加载过程中,可以遍历这些控件,找到特定的控件,或者改变某些控件的属性。

                                         页面Controls
                                       /    |     \                         //foreach(Control c in Controls)       
                            控件1   控件2  控件3            // 只判断控件1、2、3属于页面Controls
                                         /      \                          //而未涉及到下属子控件
                                子控件1   子控件2    

          为了真正做到遍历所有控件集,可以用递归的方法来实现:

          代码如下:

    public partial class _Default : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            ////没有子递归的
            //foreach (System.Web.UI.Control c in this.Page.Controls)
            //{
            //    this.result.Text += c.ToString() + "-" + c.ID + "<br>";
            //}
            ThroughChildren(this.Page);
        }

        public void ThroughChildren(System.Web.UI.Control parent)
        {
            foreach (System.Web.UI.Control c in parent.Controls)
            {
                this.result.Text += "<Li>" + c.ToString()+"</Li>";
                if (c.Controls.Count > 0) //
                {
                    this.result.Text += "<ul>";
                    ThroughChildren(c);
                    this.result.Text += "</ul>";
                }
            }
        }
    }

  • 相关阅读:
    MySQL 50条必练查询语句
    Spring MVC
    macaron 根目录默认为templates文件夹,所以如果启动目录同目录下有templates目录,要给它指定另一个文件夹
    table 表格配色
    应用连接数瓶颈解决方案
    idea修改文件的打开方式
    golang template使用
    childnode的after()方法失效问题
    vue双循环或者多循环作用于同一元素时,在外套template标签
    vue遇到组件数据变更了,但是不渲染的问题
  • 原文地址:https://www.cnblogs.com/williamwindy/p/1370419.html
Copyright © 2011-2022 走看看