我在一个panel控件中有很多控件,如:DataGridView,textBox,Button等。现我想得到Button控件,请问要怎么办啊?

解决方案 »

  1.   

    Button btn=this.panel1..FindControl("Button的ID") as Button;
    if(btn!=null)
    {
      //to do
    }
      

  2.   

    Button btn=this.panel1.FindControl("Button的ID") as Button; 
    if(btn!=null) 

      //to do 
    }
      

  3.   

    foreach(control in panel1.controls)
    {
      if(control is button)
      //handle something....
    }
      

  4.   

    意思是现在我在运行中,不知道Button的ID,能不能通过一个循环来找到呢?
      

  5.   

    panel1.Controls.Add(new button("btnP"));
      

  6.   

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

  7.   

    foreach(Control c in this.panel1.Controls) 

    if(c is Button) 


    }
      

  8.   

    给你一个参考:
    #region 清空文本框的值
            protected void ClearText(Control control)
            {
                foreach (Control ct in control.Controls)
                {
                    if (ct is TextBox)
                    {
                        if (ct.ID != "tbpnr")
                        {
                            ((TextBox)ct).Text = "";
                        }
                    }
                    else if (ct.HasControls())
                    {
                        ClearText(ct);
                    }            }
            }
            #endregion
      

  9.   

    我来纠正你们一个错误哦winform 里面 找控件 用Controls.find()
    例如:
    Button btn = this.panel1.Controls.Find("控件名")[0] as Button;//注意,找到的是个数组,所以要有索引。
    ASP.NET里面 找控件 用Control.FindControl("控件名")
    例如:
    Button btn = this.panel1.FindControl("控件名") as Button;//直接找到一个控件,不是数组哦