RT,例如我页面上有10个BUTTON,10个TEXTBOX,10个LABEL,我现在就要找出这10个BUTTON来(不知道ID的,最好是个集合,我还要循环,做判断),如何实现?

解决方案 »

  1.   

    foreach (Control c in this.Page.Controls)
    {
    if(c is Button)
    {

    }
    }
      

  2.   

    foreach (Control c in this.Controls)
    {
    if(c is Button)
    {}
    }
    容器中button,递归查询
      

  3.   

    -_-#不行啊,他只会循环一次就跳出来了,里面的那IF都没进,并且我确定页面上有Button控件
      

  4.   

    foreach (Control c in this.Controls)
    this改为你Button控件所在容器的ID
      

  5.   

    必须递归调用:
    protected void Page_Load(object sender, EventArgs e)
    {
    FindButton(this.Page);
    }
    private void FindButton(Control c)
    {
    for (int i = 0; i < c.Controls.Count; i++)
    {
    if (c.Controls[i] is Button)
    Response.Write((c.Controls[i] as Button).ID);
    else
    FindButton(c.Controls[i]);
    }
    }
      

  6.   

    这个没试过,不行的话,再执行一句:FindButton(this.Master);试试。
      

  7.   

    关键就是在你控件所属命名空间里找
    Master里的就在Master里找,gridview里的就在gridview里找
      

  8.   


    因为那只是访问了Page的第一层控件。假设页面上有1000个控件,它只是访问了10个。
      

  9.   

     (TextBox)this.Page.Master.FindControl(masterPageContentPlaceHolderID).FindControl("");