各位大侠,我想取得Gridview控件里的Button按钮的名称不一定要在Gridview控件的事件里才能取到,可以在任何事件里都可以取得。(主要就是在某些事件里想禁用Button按钮,在网上找了很多相关资料,但是都没有想要的结果,请各位大侠帮帮忙,谢谢!)
    protected void TabButton2_Click(object sender, EventArgs e)
    {
        tab1.Style.Add("display", "none");
        tab2.Style.Add("display", "block");
        tab3.Style.Add("display", "none");
        tab4.Style.Add("display", "none");
        TabButton1.CssClass = "bbb";
        TabButton2.CssClass = "aaa";
        TabButton3.CssClass = "bbb";
        TabButton4.CssClass = "bbb";            这是我取的Button控件方法,但是报错,找不到Button1
           // Button bt = new Button();
           // bt = (Button)Gridview1.FindControl("Button1");
           // bt.Enabled = false;        }

解决方案 »

  1.   

     查找GridView1各行的按钮
    foreach (GridViewRow r in GridView1.Rows)
     { 
                
         Button bt = r.FindControl("Button1") as Button; 
           if(bt!=null)
           {
              bt.Enabled = false; 
           }
    }
      

  2.   

    不在gridview的事件中找控件就要用循环来找
      

  3.   

    for (int i = 0; i < this.GridView1.Rows.Count; i++)
            {
                Button btn = (Button)this.GridView1.Rows[i].FindControl("ButtonID");
                btn.Enabled = false;
            }
      

  4.   


     // Button bt = new Button(); 
              // bt = (Button)Gridview1.FindControl("Button1"); 
              // bt.Enabled = false; 换掉就可以了.
    如果你要根据某些值设置按纽是否可用.
    你还可以根据GridView中行GridViewRow里面的列值判断是否可用
      

  5.   

    但是我是根据条件来查询的,当我的状态为0时的button1是要被禁用的,如果不为0时,就不被禁用
        protected void TabButton2_Click(object sender, EventArgs e)
        {
            tab1.Style.Add("display", "none");
            tab2.Style.Add("display", "block");
            tab3.Style.Add("display", "none");
            tab4.Style.Add("display", "none");
            TabButton1.CssClass = "bbb";
            TabButton2.CssClass = "aaa";
            TabButton3.CssClass = "bbb";
            TabButton4.CssClass = "bbb";         //sql语句(条件查询)
            string sql = "SELECT Leave.Leave_id AS Leave_id, Leave.FillTime AS FillTime,users.users_name AS users_name, Leave.department_name AS department_name,Leave.LeaveFirstTime AS LeaveFirstTime, Leave.LeaveEndTime AS LeaveEndTime,Leave.LeaveSumTime AS LeaveSumTime, Leave.LeaveBecause AS LeaveBecause,Leave.ManagerIfRatify AS ManagerIfRatify, Leave.ManagerName AS ManagerName,Leave.GManagerIfRatify AS GManagerIfRatify,Leave.GManagerName AS GManagerName, Leave.Executivedep AS Executivedep, CASE Leave.LeaveState WHEN 0 THEN '已审核' WHEN 1 THEN '待审核' WHEN 2 THEN '审核未通过' ELSE '还没有开始查看'END AS LeaveState FROM Leave INNER JOIN users ON Leave.users_id = users.users_id WHERE (LeaveState = 0)";        SqlDataAdapter da = new SqlDataAdapter(sql, DAL.Sqlconn.getcon());
            DataSet ds = new DataSet();
            da.Fill(ds);        if (ds.Tables[0].Rows.Count > 0)
            {
               foreach (GridViewRow r in GridView1.Rows) 
                {   
                Button bt = r.FindControl("Button1") as Button; 
                if(bt!=null) 
               { 
                bt.Enabled = false; 
              }                 //Button bt = new Button();
                    //bt = (Button)LeaveView.FindControl("btnSelect");
                    //bt.Enabled = false;
                }
            }
        }
    请问带条件选择性要怎么做,谢谢解答!