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>";
                }
            }
        }
    }

  • 相关阅读:
    hdu 1849 Rabbit and Grass(nim)
    sg函数模板
    hdu 1848 Fibonacci again and again(sg)
    hdu 1847 Good Luck in CET-4 Everybody!(sg)
    hdu 1846 Brave Game(bash)
    hdu 1517 A Multiplication Game(必胜态,必败态)
    hdu 1536/ hdu 1944 S-Nim(sg函数)
    hdu 2509 Be the Winner(anti nim)
    hdu 1907 John(anti nim)
    zoj 3965 Binary Tree Restoring(搜索)
  • 原文地址:https://www.cnblogs.com/williamwindy/p/1370419.html
Copyright © 2011-2022 走看看