遍历页面所有某或个控件

ControlCollection 控件容器 自动增长的长度

  常用操作方法  Remove(移除某个控件), Item属性,Contains(是否包含某个控件),IndexOf(获取索引)......

 

 protected void Page_Load(object sender, EventArgs e)
{   
      Clear(this.Page.Controls);

}
    //ControlCollection所有空间集合容器

public void Clear(ControlCollection allControls)
{
        foreach (Control controls in allControls)
        {
            if (controls.HasControls())
            {
                Clear(controls.Controls);//递归调用
            }
            else  
            {
                if (controls is TextBox)

                {
                    ((TextBox)controls).Text = String.Empty;
                }
                if(controls is Label)
                {
                    ((Label)controls).Text = "遍历所有Label控件!";
                }
            }
        }
}

posted @ 2010-11-21 21:42  SpringNumb  阅读(153)  评论(0)    收藏  举报