<asp:TemplateField HeaderText="操作">
     <ItemTemplate>
        &nbsp; <input type="hidden" id="SelectedID" runat="server"   value='<%#Eval("zhanhui_id") %>' name="SelectedID"/>
        <input type="checkbox" id="chkSelectBox" name="chkSelectBox" />
     </ItemTemplate> 
     <ItemStyle HorizontalAlign="Center" />  
</asp:TemplateField>全选<input id="CheckAll" onclick="return select_deselectAll (this.checked, this.id)" tabindex="0"
type="checkbox" title="点击全选或反全选当前页所有信息" />&nbsp;&nbsp;&nbsp;&nbsp;
<asp:Button id="btnDel" runat="server" Text="删 除" OnClick="btnDel_Click"></asp:Button><script language="javascript" type="text/javascript">
 
  //CheckBox全选And反全选
  function select_deselectAll (chkVal, idVal) 
  {
   var frm = document.forms[0];
   for (i=0; i<frm.length; i++) 
   {
    if (idVal.indexOf ('CheckAll') != -1)
    {
     if(chkVal == true) 
     {
      frm.elements[i].checked = true;
     } 
     else 
     {
      frm.elements[i].checked = false;
     }
    } 
    else if (idVal.indexOf('DeleteThis') != -1) 
    {
     if(frm.elements[i].checked == false) 
     {
      frm.elements[1].checked = false;
     }
    }
   }
}
</script>protected void btnDel_Click(object sender, EventArgs e)
    {
        string s = "";
        foreach (GridViewRow dgi in this.GridView1.Rows)
        {            CheckBox chkBox = (CheckBox)dgi.Cells[0].FindControl("chkSelectBox"); 
            if (chkBox.Checked)
            {
                s += ((HtmlInputHidden)dgi.FindControl("SelectedID")).Value.ToString() + ",";
            }
        }
        if (s.Length - 1 > 0)
        {
            SqlConnection con = new SqlConnection(ConfigurationManager.AppSettings["Connstr"]);
            SqlCommand comm = new SqlCommand("delete from gonggaoxinxi where zhanhui_id in (", con);
            comm.CommandText += s.Substring(0, s.Length - 1) + ")"; //删除最后的逗号
            con.Open();
            comm.ExecuteNonQuery();
            con.Close();
        }
        //this.BindToDataGrid();
    }红色的两行出现在错误,好象没取到值......

解决方案 »

  1.   


    CheckBox chkBox = (CheckBox)dgi.Cells[0].FindControl("chkSelectBox"); 
    if (chkBox.Checked)
      

  2.   

              <input type="checkbox" id="chkSelectBox" name="chkSelectBox" />
    改成=》   <input type="checkbox" runat="server" id="chkSelectBox" name="chkSelectBox" />          CheckBox chkBox = (CheckBox)dgi.Cells[0].FindControl("chkSelectBox"); 
    改成=》   CheckBox chkBox = (CheckBox)dgi.FindControl("chkSelectBox"); 取值要在服务器控件上取,要不服务器上没有回传的值,取不到