protected void btnTest_Click(object sender, EventArgs e)
    {
        int intTest = 0;
        for (int j = 0; j < gvTest.Rows.Count; j++)
        {
            CheckBox CB = (CheckBox)gvTest.Rows[j].FindControl("cbTest");
            if (CB.Checked)
            {
                intTest = intTest + 1;
                CB.Checked = true;                
            }    
        }
    }
上面cbTest是GridView中定义的CheckBox,gvTest是cbTest所在的GridView,可是不管cbTest有没有勾选,CB.Checked一直都是false,不知道是什么原因,我上网查了,好多地方都说GridView中操作CheckBox就是这样的呀,那位高手帮我看看啊,谢谢!!!

解决方案 »

  1.   


    protected void CheckBox2_CheckedChanged(object sender, EventArgs e)
        {
            for (int i = 0; i <=GridView1.Rows.Count - 1;i++)
            {
                CheckBox cbox = (CheckBox)GridView1.Rows[i].FindControl("CheckBox1");
                if (CheckBox2.Checked)
                {
                    cbox.Checked = true;
                }
                else
                    cbox.Checked = false;
            }
        }
      

  2.   

    可以看一下CheckBox的EnableViewState属性是怎么设置的。
      

  3.   

    EnableViewState置成true和false没有任何区别
      

  4.   

    这个方法执行完后,页面显示CheckBox被选中了,但是cbox.Checked 还是false
      

  5.   

    回复shadowjl ,这个方法执行完后,页面显示CheckBox被选中了,但是cbox.Checked 还是false
      

  6.   

    CheckBox CB = (CheckBox)gvTest.Rows[j].Cells[0].FindControl("cbTest"),0為你的checkbox所在的列,改一下看看
      

  7.   

    回复linux_ch ,这样写,还是不行的
      

  8.   

    CheckBox CB = (CheckBox)gvTest.Rows[j].FindControl("cbTest"); 
    這裡能不能找到cbTest?
    debug看看
      

  9.   

    参考看看:http://blog.csdn.net/insus/archive/2008/03/09/2159352.aspx
      

  10.   

    谢谢insus,我现在的问题是,能实现全选功能,但是即使我已经在页面上全选了所有CheckBox,但是在后台判断时,CB.Checked依然是false呀
      

  11.   

    回复linux_ch ,CheckBox CB = (CheckBox)gvTest.Rows[j].FindControl("cbTest"); 
    是可以找到cbTest的,只是CB.Checked永远都是false
      

  12.   

    看下你的Page_load中的代码,点击按钮会首先触发回调事件,在执行你的按钮事件
      

  13.   


     protected void Button1_Click(object sender, EventArgs e)
        {
            SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["Myconn"].ConnectionString);
            SqlCommand com;
            for(int i=0;i<=GridView1.Rows.Count-1;i++)
            {
                CheckBox cbox=(CheckBox)GridView1.Rows[i].FindControl("CheckBox1");
                if(cbox.Checked)
                {
                    string sql="update yuangong set 在职情况ID='"+this.DropDownList2.SelectedValue+"'"+"where 职工号='"+GridView1.DataKeys[i].Value+"'";
                    com=new SqlCommand(sql,conn);
                    conn.Open();
                    com.ExecuteNonQuery();
                    conn.Close();
                }
               
            }
            setbind(Convert.ToInt32(this.DropDownList1.SelectedValue));
        }
    选中之后
    我一直这样做没发现有什么问题~~~
      

  14.   

    看一下你的畫面上是不是datagridview放在了panel上面,看看panel的屬設置...具體忘記是什麽設置了
      

  15.   

    是不是Page_Load里面没有加if(IspostBack),你就在page_load里把所有的CheckBox的Checked置为false了.
    这样你后面无论怎样,所有CheckBox的Checked都是false
      

  16.   


    protected void btnTest_Click(object sender, EventArgs e) 
        { 
            int intTest = 0; 
            for (int j = 0; j < gvTest.Rows.Count; j++) 
            { 
                CheckBox CB = (CheckBox)gvTest.Rows[j].FindControl("cbTest"); 
                if (CB.Checked == true) 
                { 
                    intTest = intTest + 1; 
                    CB.Checked = true;                
                }    
            } 
        }