none
删除gridview 数据前判断是否选中checkbox RRS feed

  • 问题

  • 需求:一个gridview 标题里有个全选的checkbox 每个数据项里各有一个checkbox  可以实现全选和单个选项。。最下面有个删除按钮是button 。删除数据前判断是否选择要删除的数据。如果没选择,提示用户没有没有选择要删除的项目,如果选择 则删除选择的项目,我的代码执行却有问题。有的时候选择的我执行删除却提示没选择。。没选择 删除却把数据删除啦。。代码如下
       //全选代码  protected void CheckAll_CheckedChanged(object sender, EventArgs e)
        {
            for (int i = 0; i < GVCp.Rows.Count; i++)
            {
                CheckBox cb = (CheckBox)GVCp.Rows[i].FindControl("CheckSingle");
                if (!cb.Checked)
                {
                    cb.Checked = true;
                }
                else
                {
                    cb.Checked = false;
                }
            }
        }
    //删除选定
        protected void LnkDelete_Click(object sender, EventArgs e)
        {
            for (int i = 0; i < GVCp.Rows.Count; i++)
            {
                bool isChecked = ((CheckBox)GVCp.Rows[i].FindControl("CheckSingle")).Checked;
                if (isChecked)
                {
                    isSelect = true;
                    LnkDelete.Attributes["OnClick"] = "return confirm('您真的要删除吗?')";
                    string strSql = "Delete From Company Where [id]=" + GVCp.DataKeys[i].Value + "";
                    DB.RunExecuteNonQuery(strSql);
                }
                else
                {
                    Response.Write("<script>alert('请选择要删除的项')</script>");
                }
            }
            Response.Write("<script language=javascript>window.location='Company.aspx'</script>");
        }
        <asp:GridView ID="GVCp" runat="server" AllowPaging="True" AutoGenerateColumns="False" EmptyDataText=" 暂无数据!" Width="100%" DataKeyNames="id" Height="40px" Font-Size="Medium" AlternatingRowStyle-Height="30"  HeaderStyle-HorizontalAlign="Center" RowStyle-HorizontalAlign="Center" DataSourceID="ACDSCp" OnRowDataBound="GVCp_RowDataBound" >
    
                    <AlternatingRowStyle BackColor="#F7F7F7" Height="24px" />
    
                    <RowStyle Height="24px" HorizontalAlign="Center" />
    
                    <HeaderStyle Height="26px" HorizontalAlign="Center" />
    
                    <PagerSettings PageButtonCount="1" />
    
                    <PagerStyle Height="0px" />
    
                    <PagerTemplate > </PagerTemplate><Columns>
    
                        <asp:TemplateField HeaderText="选取">
    
                            <EditItemTemplate>
    
                            </EditItemTemplate>
    
                            <HeaderTemplate>
    
                                <asp:CheckBox ID="CheckAll" runat="server" AutoPostBack="True" OnCheckedChanged="CheckAll_CheckedChanged" />
    
                            </HeaderTemplate>
    
                            <ItemStyle HorizontalAlign="Center" />
    
                            <HeaderStyle BackColor="#DADADA" ForeColor="White" HorizontalAlign="Center" />
    
                            <ItemTemplate>
    
                                <asp:CheckBox ID="CheckSingle" runat="server" />
    
                            </ItemTemplate>
    
                    </asp:TemplateField>
    
                        <asp:TemplateField HeaderText="公司名称">
    
                            <EditItemTemplate>
    
                                <asp:TextBox ID="TextBox2" runat="server" Text='<%# Bind("Cp_name") %>'></asp:TextBox>
    
                            </EditItemTemplate>
    
                            <HeaderStyle BackColor="#DADADA" />
    
                            <ItemTemplate>
    
                                <asp:Label ID="Label2" runat="server" Text='<%#Strings.CutString(Eval("Cp_name").ToString(),10)%>'></asp:Label>
    
                            </ItemTemplate>
    
                        </asp:TemplateField>
    
                        <asp:TemplateField HeaderText="项目名称">
    
                            <EditItemTemplate>
    
                                <asp:TextBox ID="TextBox3" runat="server" Text='<%# Bind("Cp_Item") %>'></asp:TextBox>
    
                            </EditItemTemplate>
    
                            <HeaderStyle BackColor="#DADADA" />
    
                            <ItemTemplate>
    
                                <asp:Label ID="Label3" runat="server" Text='<%#Strings.CutString(Eval("Cp_Item").ToString(),12)%>'></asp:Label>
    
                            </ItemTemplate>
    
                        </asp:TemplateField>
    
                        <asp:TemplateField HeaderText="公司地址">
    
                            <EditItemTemplate>
    
                                <asp:TextBox ID="TextBox1" runat="server" Text='<%# Bind("Cp_Address") %>'></asp:TextBox>
    
                            </EditItemTemplate>
    
                            <HeaderStyle BackColor="#DADADA" />
    
                            <ItemTemplate>
    
                                <asp:Label ID="Label1" runat="server" Text='<%#Strings.CutString(Eval("Cp_Address").ToString(),30)%>'></asp:Label> 
    
                            </ItemTemplate>
    
                        </asp:TemplateField>
    
                        <asp:BoundField DataField="Cp_Tel" HeaderText="联系电话" >
    
                            <HeaderStyle BackColor="#DADADA" />
    
                        </asp:BoundField>
    
                    <asp:TemplateField HeaderText="点选操作" ShowHeader="False">
    
                      <ItemTemplate>
    
                          &nbsp;<a class="slan" href="Add_New.aspx?id=<%# Eval("id") %>"> 修改</a> | &nbsp;<a class="slan" href="Company_Details.aspx?id=<%# Eval("id") %>">详细</a> 
    
                      </ItemTemplate>
    
                      <HeaderStyle BackColor="#DADADA" HorizontalAlign="Center" Width="80px" />
    
                      <ItemStyle  CssClass="yellow" ForeColor="Red" Wrap="True" HorizontalAlign="Center" />
    
                    </asp:TemplateField>
    
                    </Columns>
    
                  </asp:GridView>
    
    
    总感觉我这个在服务端执行选中的代码 是不是挥发了导致删除选中不准确。
    求个能解决我的需求完整代码谢谢啦
    不用羡慕----那,只是个传说!
    2009年4月9日 10:42

答案

  • <%@ Page Language="C#" %>
    
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    
    <script runat="server">
    
    
      protected void Page_Load(object sender, EventArgs e)
      {
        LnkDelete.Attributes["OnClick"] = "return confirm('您真的要删除吗?')";
      }
      protected void CheckAll_CheckedChanged(object sender, EventArgs e)
      {
        CheckBox ck = sender as CheckBox;
        for (int i = 0; i < GVCp.Rows.Count; i++)
        {
          CheckBox cb = (CheckBox)GVCp.Rows[i].FindControl("CheckSingle");
    
          cb.Checked = ck.Checked;
    
        }
      }
    
      protected void GVCp_RowDataBound(object sender, GridViewRowEventArgs e)
      {
    
      }
    
      protected void LnkDelete_Click(object sender, EventArgs e)
      {
        bool isSelect = false;
        for (int i = 0; i < GVCp.Rows.Count; i++)
        {
          bool isChecked = ((CheckBox)GVCp.Rows[i].FindControl("CheckSingle")).Checked;
          if (isChecked)
          {
            isSelect = true;
    
            string strSql = "Delete From Company Where [id]=" + GVCp.DataKeys[i].Value + "";
            // DB.RunExecuteNonQuery(strSql);
          }
    
        }
        if (!isSelect)
        {
          Response.Write("<script>alert('请选择要删除的项')</" + "script>");
        }
        else
        {
          Response.Write("<script>alert('您已经选择了。')</" + "script>");
        }
        //Response.Write("<script language=javascript>window.location='Company.aspx'</"+"script>");
    
      }
    </script>
    
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head runat="server">
      <title></title>
    </head>
    <body>
      <form id="form1" runat="server">
      <asp:GridView ID="GVCp" runat="server" AllowPaging="True" AutoGenerateColumns="False"
        EmptyDataText=" 暂无数据!" Width="100%" DataKeyNames="CategoryID" Height="40px" Font-Size="Medium"
        AlternatingRowStyle-Height="30" HeaderStyle-HorizontalAlign="Center" RowStyle-HorizontalAlign="Center"
        DataSourceID="ACDSCp" OnRowDataBound="GVCp_RowDataBound">
        <AlternatingRowStyle BackColor="#F7F7F7" Height="24px" />
        <RowStyle Height="24px" HorizontalAlign="Center" />
        <HeaderStyle Height="26px" HorizontalAlign="Center" />
        <PagerSettings PageButtonCount="1" />
        <PagerStyle Height="0px" />
        <PagerTemplate>
        </PagerTemplate>
        <Columns>
          <asp:TemplateField HeaderText="选取">
            <EditItemTemplate>
            </EditItemTemplate>
            <HeaderTemplate>
              <asp:CheckBox ID="CheckAll" runat="server" AutoPostBack="True" OnCheckedChanged="CheckAll_CheckedChanged" />
            </HeaderTemplate>
            <ItemStyle HorizontalAlign="Center" />
            <HeaderStyle BackColor="#DADADA" ForeColor="White" HorizontalAlign="Center" />
            <ItemTemplate>
              <asp:CheckBox ID="CheckSingle" runat="server" />
            </ItemTemplate>
          </asp:TemplateField>
          <asp:TemplateField HeaderText="公司名称">
            <EditItemTemplate>
              <asp:TextBox ID="TextBox2" runat="server" Text='<%# Bind("CategoryName") %>'></asp:TextBox>
            </EditItemTemplate>
            <HeaderStyle BackColor="#DADADA" />
            <ItemTemplate>
              <asp:Label ID="Label2" runat="server" Text='<%#Eval("CategoryName")%>'></asp:Label>
            </ItemTemplate>
          </asp:TemplateField>
          <asp:TemplateField HeaderText="公司地址">
            <EditItemTemplate>
              <asp:TextBox ID="TextBox1" runat="server" Text='<%# Bind("Description") %>'></asp:TextBox>
            </EditItemTemplate>
            <HeaderStyle BackColor="#DADADA" />
            <ItemTemplate>
              <asp:Label ID="Label1" runat="server" Text='<%#Eval("Description")%>'></asp:Label>
            </ItemTemplate>
          </asp:TemplateField>
          <asp:BoundField DataField="CategoryName" HeaderText="联系电话">
            <HeaderStyle BackColor="#DADADA" />
          </asp:BoundField>
          <asp:TemplateField HeaderText="点选操作" ShowHeader="False">
            <ItemTemplate>
              &nbsp;<a class="slan" href="Add_New.aspx?id=<%# Eval("CategoryID") %>"> 修改</a> |
              &nbsp;<a class="slan" href="Company_Details.aspx?id=<%# Eval("CategoryID") %>">详细</a>
            </ItemTemplate>
            <HeaderStyle BackColor="#DADADA" HorizontalAlign="Center" Width="80px" />
            <ItemStyle CssClass="yellow" ForeColor="Red" Wrap="True" HorizontalAlign="Center" />
          </asp:TemplateField>
        </Columns>
      </asp:GridView>
      <asp:SqlDataSource ID="ACDSCp" runat="server" ConnectionString="Data Source=.;Initial Catalog=Northwind;Integrated Security=True"
        ProviderName="System.Data.SqlClient" SelectCommand="SELECT [CategoryID], [CategoryName], [Description] FROM [Categories]">
      </asp:SqlDataSource>
      <asp:LinkButton ID="LnkDelete" runat="server" OnClick="LnkDelete_Click">删除</asp:LinkButton>
      </form>
    </body>
    </html>
    

    孟宪会
    • 已标记为答案 极度 2009年4月10日 6:09
    2009年4月9日 14:22
    版主

全部回复

  • 你 的代码正好晚了一个节拍

    LnkDelete.Attributes["OnClick"] = "return confirm('您真的要删除吗?')";
    应该加在绑定的事件里面。你的代码在服务器端先执行了,再提市这个就晚了。


    孟宪会
    2009年4月9日 13:42
    版主
  • 总是我的代码还是有问题。第一次什么不选择 删除提示没选择 。然后我选择checkox 在提交也提示我没选择
    不用羡慕----那,只是个传说!
    2009年4月9日 13:53
  • <%@ Page Language="C#" %>
    
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    
    <script runat="server">
    
    
      protected void Page_Load(object sender, EventArgs e)
      {
        LnkDelete.Attributes["OnClick"] = "return confirm('您真的要删除吗?')";
      }
      protected void CheckAll_CheckedChanged(object sender, EventArgs e)
      {
        CheckBox ck = sender as CheckBox;
        for (int i = 0; i < GVCp.Rows.Count; i++)
        {
          CheckBox cb = (CheckBox)GVCp.Rows[i].FindControl("CheckSingle");
    
          cb.Checked = ck.Checked;
    
        }
      }
    
      protected void GVCp_RowDataBound(object sender, GridViewRowEventArgs e)
      {
    
      }
    
      protected void LnkDelete_Click(object sender, EventArgs e)
      {
        bool isSelect = false;
        for (int i = 0; i < GVCp.Rows.Count; i++)
        {
          bool isChecked = ((CheckBox)GVCp.Rows[i].FindControl("CheckSingle")).Checked;
          if (isChecked)
          {
            isSelect = true;
    
            string strSql = "Delete From Company Where [id]=" + GVCp.DataKeys[i].Value + "";
            // DB.RunExecuteNonQuery(strSql);
          }
    
        }
        if (!isSelect)
        {
          Response.Write("<script>alert('请选择要删除的项')</" + "script>");
        }
        else
        {
          Response.Write("<script>alert('您已经选择了。')</" + "script>");
        }
        //Response.Write("<script language=javascript>window.location='Company.aspx'</"+"script>");
    
      }
    </script>
    
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head runat="server">
      <title></title>
    </head>
    <body>
      <form id="form1" runat="server">
      <asp:GridView ID="GVCp" runat="server" AllowPaging="True" AutoGenerateColumns="False"
        EmptyDataText=" 暂无数据!" Width="100%" DataKeyNames="CategoryID" Height="40px" Font-Size="Medium"
        AlternatingRowStyle-Height="30" HeaderStyle-HorizontalAlign="Center" RowStyle-HorizontalAlign="Center"
        DataSourceID="ACDSCp" OnRowDataBound="GVCp_RowDataBound">
        <AlternatingRowStyle BackColor="#F7F7F7" Height="24px" />
        <RowStyle Height="24px" HorizontalAlign="Center" />
        <HeaderStyle Height="26px" HorizontalAlign="Center" />
        <PagerSettings PageButtonCount="1" />
        <PagerStyle Height="0px" />
        <PagerTemplate>
        </PagerTemplate>
        <Columns>
          <asp:TemplateField HeaderText="选取">
            <EditItemTemplate>
            </EditItemTemplate>
            <HeaderTemplate>
              <asp:CheckBox ID="CheckAll" runat="server" AutoPostBack="True" OnCheckedChanged="CheckAll_CheckedChanged" />
            </HeaderTemplate>
            <ItemStyle HorizontalAlign="Center" />
            <HeaderStyle BackColor="#DADADA" ForeColor="White" HorizontalAlign="Center" />
            <ItemTemplate>
              <asp:CheckBox ID="CheckSingle" runat="server" />
            </ItemTemplate>
          </asp:TemplateField>
          <asp:TemplateField HeaderText="公司名称">
            <EditItemTemplate>
              <asp:TextBox ID="TextBox2" runat="server" Text='<%# Bind("CategoryName") %>'></asp:TextBox>
            </EditItemTemplate>
            <HeaderStyle BackColor="#DADADA" />
            <ItemTemplate>
              <asp:Label ID="Label2" runat="server" Text='<%#Eval("CategoryName")%>'></asp:Label>
            </ItemTemplate>
          </asp:TemplateField>
          <asp:TemplateField HeaderText="公司地址">
            <EditItemTemplate>
              <asp:TextBox ID="TextBox1" runat="server" Text='<%# Bind("Description") %>'></asp:TextBox>
            </EditItemTemplate>
            <HeaderStyle BackColor="#DADADA" />
            <ItemTemplate>
              <asp:Label ID="Label1" runat="server" Text='<%#Eval("Description")%>'></asp:Label>
            </ItemTemplate>
          </asp:TemplateField>
          <asp:BoundField DataField="CategoryName" HeaderText="联系电话">
            <HeaderStyle BackColor="#DADADA" />
          </asp:BoundField>
          <asp:TemplateField HeaderText="点选操作" ShowHeader="False">
            <ItemTemplate>
              &nbsp;<a class="slan" href="Add_New.aspx?id=<%# Eval("CategoryID") %>"> 修改</a> |
              &nbsp;<a class="slan" href="Company_Details.aspx?id=<%# Eval("CategoryID") %>">详细</a>
            </ItemTemplate>
            <HeaderStyle BackColor="#DADADA" HorizontalAlign="Center" Width="80px" />
            <ItemStyle CssClass="yellow" ForeColor="Red" Wrap="True" HorizontalAlign="Center" />
          </asp:TemplateField>
        </Columns>
      </asp:GridView>
      <asp:SqlDataSource ID="ACDSCp" runat="server" ConnectionString="Data Source=.;Initial Catalog=Northwind;Integrated Security=True"
        ProviderName="System.Data.SqlClient" SelectCommand="SELECT [CategoryID], [CategoryName], [Description] FROM [Categories]">
      </asp:SqlDataSource>
      <asp:LinkButton ID="LnkDelete" runat="server" OnClick="LnkDelete_Click">删除</asp:LinkButton>
      </form>
    </body>
    </html>
    

    孟宪会
    • 已标记为答案 极度 2009年4月10日 6:09
    2009年4月9日 14:22
    版主