木棉

导航

页面遍历TextBox控件

方法一:

   foreach (Control control in this.Form.Controls)
       {
            if (control.GetType() == typeof(TextBox))
            {
                TextBox txtbox = new TextBox();
                txtbox = (TextBox)this.Page.FindControl(control.ID);
                txtbox.Text = string.Empty;
            }
        }

-----------------------------------------------------------------------------------------------------------------------

方法二:

  for (int j = 0; j < this.Controls.Count; j++)
     {
            foreach (object obj in Page.Controls[j].Controls)
            {

                if (obj is TextBox)
                {
                    TextBox txt = (System.Web.UI.WebControls.TextBox)obj;
                    txt.Text = string.Empty;  
                }
            }
     }

-----------------------------------------------------------------------------------------------------------------------

方法三:

1、定义递归遍历方法

 private void FindAllTextBoxByPageControl(ControlCollection controls)
    {
        for (int i = 0; i < controls.Count;i++ )
        {
            if (controls[i].GetType() == typeof(TextBox))
            {
                ((TextBox)controls[i]).Text = string.Empty;
            }  
            if(controls[i].HasControls())
            {
                FindAllTextBoxByPageControl(controls[i].Controls);
            }
        }
    }

2、方法调用

FindAllTextBoxByPageControl(this.Page.Controls);

posted on 2011-08-07 16:29  木棉  阅读(274)  评论(0编辑  收藏  举报