index.aspx部分代码
<asp:TemplateColumn>
<HeaderTemplate>
<input type="checkbox" id="CheckAll" onclick="return deselect(this.checked,this.id);" Runat="server" />
</HeaderTemplate>
<ItemTemplate>
<input type="checkbox" id="DeleteThis" onclick="return deselect(this.checked,this.id);"
Runat="server" />
</ItemTemplate>
</asp:TemplateColumn>index.aspx.cs中代码 private void Button_del_Click(object sender, System.EventArgs e)
{
string chkids="";
    bool chk_select=false;
foreach(DataGridItem i in DataGrid1.Items)
{
   Boolean DelBox = ((CheckBox)i.FindControl("DeleteThis")).Checked; if(DelBox)
{
   chk_select=true;
                 chkids=chkids+i.Cells[0].Text.ToString()+",";
}

} if(chk_select)
{
string dbpath=@"student.mdb";
string connStr="PROVIDER=Microsoft.Jet.OLEDB.4.0;DATA Source="+Server.MapPath(dbpath)+";";
OleDbConnection myconn=new OleDbConnection(connStr);
string sqlstr="delete * from info where id in (" + chkids.Substring (0, chkids.LastIndexOf (",")) + ")";
OleDbCommand mycommdel=new OleDbCommand(sqlstr,myconn);
myconn.Open();
mycommdel.ExecuteNonQuery();
myconn.Close();
bind();       

}
---------------------------------------------------
指定的转换无效。 
说明: 执行当前 Web 请求期间,出现未处理的异常。请检查堆栈跟踪信息,以了解有关该错误以及代码中导致错误的出处的详细信息。 异常详细信息: System.InvalidCastException: 指定的转换无效。源错误: 
行 138: foreach(DataGridItem i in DataGrid1.Items)
行 139: {
行 140:    Boolean DelBox = ((CheckBox)i.FindControl("DeleteThis")).Checked;
行 141:
行 142: if(DelBox)
 

解决方案 »

  1.   

    改为如下:int mycount = this.DataGrid1.Items.Count;
    for(int i=0;i<mycount;i++)
    {
    Boolean DelBox = ((CheckBox)this.DataGrid1.Items[i].FindControl("DeleteThis")).Checked;
    if(DelBox == true)
    {
    chk_select=true;
    chkids=chkids+i.Cells[0].Text.ToString()+",";
    }
    }
      

  2.   

    为什么不直接用web控件checkbox?
      

  3.   

    因为要用 javascript 实现全选,web 控件好象不可以用javascript
      

  4.   

    直接用Request["DeleteThis"]就可以得到你选择的chk,不用foreach了chkids = Request["DeleteThis"].ToString();
    在删除按钮上加一个客户端事件,判断是否有复选框被选中,如果有则执行删除操作,如果没有则alert
    或者在.cs中判断chkids是否为空
      

  5.   

    TO:  hchxxzx(NET?摸到一点门槛) 
    还是不行
      

  6.   

    to:lovefootball(蓝色咖啡)
    能不能详细点,小弟惭愧。。
      

  7.   

    谢谢大家,问题解决了。
    我用的htmlcontrol , CheckBox 是属于 WebCotrol 的,改成HtmlInputCheckBox就可以了。