protected void Button1_Click(object sender, EventArgs e)
    {
        foreach (GridViewRow gi in GridView1.Rows)
        {
            Control ctrl = gi.Cells[0].FindControl("CheckBox1");
            if (ctrl != null)
            {
                CheckBox cb = ctrl as CheckBox;
                if (Culture != null)
                {
                    if (cb.Checked == true)
                    {
                        Members mb = new Members();
                        mb.UserID = Convert.ToInt32(gi.Cells[0].Text);                        DAL dal = new DAL();
                        int b = dal.Delete(mb);
                        string msg = "";                        if (b >= 1)
                        {
                            msg = "删除成功!!!";
                        }                        else
                        {
                            msg = "删除失败!!";
                        }                        Common.ShowMsg(msg, this);
                        this.GridView1.DataBind();                    }
                }
            }
 
        }
    }写完  运行后对CheckBox  打勾后ctrl的值一直是False  这是为什么额 ,我自己之前做的都可以额,现在做就不行 想了很久 就是改不出来 在线等呀!!

解决方案 »

  1.   

    你判断的复选框是你打勾的复选框吗?
    你先把复选框默认都设置为True,看看结果如何。
      

  2.   

    贴出你的前台代码
    你的checkbox是否用的服务器控件?
      

  3.   

    是不是没写这个
     if (!IsPostBack)
                {
    //GridView1绑定..
    }
      

  4.   

    <%@ Page Language="C#" AutoEventWireup="true" CodeFile="GridView.aspx.cs" Inherits="GridView" %><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml" >
    <head runat="server">
        <title>无标题页</title>
    </head>
    <body>
        <form id="form1" runat="server">
        <div align="center">
            <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" >
                <Columns>
                    <asp:BoundField DataField="UserID" HeaderText="ID" />
                    <asp:BoundField DataField="UserName" HeaderText="用户名" />
                    <asp:BoundField DataField="UserSex" HeaderText="性别" />
                    <asp:BoundField DataField="Email" HeaderText="电子邮箱" />
                    <asp:BoundField DataField="Reloe" HeaderText="角色" />
                    <asp:BoundField DataField="RegTime" HeaderText="注册时间" />
                    <asp:TemplateField HeaderText="选项">
                        <ItemTemplate>
                            <asp:CheckBox ID="Check" runat="server" />
                        </ItemTemplate>
                    </asp:TemplateField>
                    <asp:TemplateField HeaderText="删除">
                        <ItemTemplate>
                            <asp:Button ID="butDel" runat="server" Text="删除" CommandArgument='<%# Eval("UserID") %> ' CommandName="del" />
                        </ItemTemplate>
                    </asp:TemplateField>
                </Columns>
            </asp:GridView>
        <asp:Button ID="Button1" runat="server" OnClick="Button1_Click" Text="删除" />
        </div>
            
        </form>
    </body>
    </html>
      

  5.   

    要去掉表头表尾的行,因为表头表尾没有checkbox,只有内容行有,所以要在foreach里加:
    if (gi.RowType==DataControlRowType.DataRow)
      

  6.   


    foreach (GridViewRow item in this.GridView1.Rows)
      {
      HtmlInputCheckBox hicb = item.Cells[0].FindControl("cbSelect") as HtmlInputCheckBox;
      if (hicb.Checked)
      {..}
    }还有就是把页面上的<asp:CheckBox ID="cbSelect" runat="server" />   
    改成<input id="cbSelect" value='<%# Eval("id") %>' runat="server" type="checkbox" />
      

  7.   

      //是不是少一个this.GridView1.DataSource="";
      Common.ShowMsg(msg, this);
      this.GridView1.DataBind();看看数据库中删除成功没有。