GridView模板列中添加CheckBox 实现全选、删除

  1. <%@ Page Language="C#" AutoEventWireup="true"  CodeFile="Default.aspx.cs" Inherits="_Default" %>
  2. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  3. <html xmlns="http://www.w3.org/1999/xhtml" >
  4. <head runat="server">
  5.     <title>GridView模板列中添加CheckBox 实现全选、删除</title>
  6. </head>
  7. <body>
  8.     <form id="form1" runat="server">
  9.     <div>
  10.         <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" CellPadding="10" ForeColor="#333333" PageSize="3" AllowSorting="True" BackColor="White" BorderColor="#CCCCCC" Font-Size="9pt" BorderStyle="None" BorderWidth="1px"> 
  11.             <FooterStyle BackColor="White" ForeColor="#000066" />
  12.             <Columns>
  13.                 <asp:TemplateField>
  14.                     <ItemTemplate>
  15.                         <asp:CheckBox ID="CheckBox1" runat="server" />
  16.                     </ItemTemplate>
  17.                 </asp:TemplateField> 
  18.                 <asp:BoundField DataField="ID" HeaderText="用户ID" ReadOnly="True" />
  19.                 <asp:BoundField DataField="Name" HeaderText="用户姓名" />
  20.                 <asp:BoundField DataField="Address" HeaderText="家庭住址" />
  21.             </Columns>
  22.             <RowStyle ForeColor="#000066" />
  23.             <SelectedRowStyle BackColor="#669999" Font-Bold="True" ForeColor="White" />
  24.             <PagerStyle BackColor="White" ForeColor="#000066" HorizontalAlign="Left" />
  25.             <HeaderStyle BackColor="#006699" Font-Bold="True" ForeColor="White" />
  26.         </asp:GridView>
  27.         <asp:CheckBox ID="CheckBox2" runat="server" AutoPostBack="True" Font-Size="9pt" OnCheckedChanged="CheckBox2_CheckedChanged" Text="全选" />
  28.         <asp:Button ID="Button1" runat="server" Font-Size="9pt" Text="取消" OnClick="Button1_Click" />
  29.         <asp:Button ID="Button2" runat="server" Font-Size="9pt" Text="删除" OnClick="Button2_Click" />
  30.     </div>
  31.     </form>
  32. </body>
  33. </html>
  1. using System;
  2. using System.Data;
  3. using System.Data.SqlClient;
  4. using System.Configuration;
  5. using System.Web;
  6. using System.Web.Security;
  7. using System.Web.UI;
  8. using System.Web.UI.WebControls;
  9. using System.Web.UI.WebControls.WebParts;
  10. using System.Web.UI.HtmlControls;
  11. public partial class _Default : System.Web.UI.Page 
  12. {
  13.     SqlConnection sqlConn;
  14.     SqlCommand sqlComm;
  15.     string strConn = "Data Source=(local);Database=Exercise;Uid=sa;Pwd=sa";
  16.     protected void Page_Load(object sender, EventArgs e)
  17.     {
  18.         if (!IsPostBack)
  19.         {
  20.             databind();
  21.         }
  22.     }
  23.     private void databind()
  24.     {
  25.         string strSql = "select * from myDt";
  26.         sqlConn = new SqlConnection(strConn);
  27.         SqlDataAdapter sqlDa = new SqlDataAdapter(strSql, sqlConn);
  28.         DataSet ds = new DataSet();
  29.         sqlConn.Open();
  30.         sqlDa.Fill(ds, "myDt");
  31.         GridView1.DataSource = ds;
  32.         GridView1.DataKeyNames = new string[] { "ID" };
  33.         GridView1.DataBind();
  34.         sqlConn.Close();
  35.     }
  36.     protected void CheckBox2_CheckedChanged(object sender, EventArgs e)
  37.     {
  38.         for (int i = 0; i < GridView1.Rows.Count; i++)
  39.         {
  40.             CheckBox chk = (CheckBox)GridView1.Rows[i].FindControl("CheckBox1");
  41.             if (CheckBox2.Checked == true)
  42.             {
  43.                 chk.Checked = true;
  44.             }
  45.             else
  46.             {
  47.                 chk.Checked = false;
  48.             }
  49.         }
  50.     }
  51.     protected void Button1_Click(object sender, EventArgs e)
  52.     {
  53.         CheckBox2.Checked = false;
  54.         for (int i = 0; i < GridView1.Rows.Count; i++)
  55.         {
  56.             CheckBox chk = (CheckBox)GridView1.Rows[i].FindControl("CheckBox1");
  57.             chk.Checked = false;
  58.         }
  59.     }
  60.     protected void Button2_Click(object sender, EventArgs e)
  61.     {
  62.         sqlConn = new SqlConnection(strConn);
  63.         for (int i = 0; i < GridView1.Rows.Count; i++)
  64.         {
  65.             CheckBox chk = (CheckBox)GridView1.Rows[i].FindControl("CheckBox1");
  66.             if(chk.Checked == true)
  67.             {
  68.                 string strSql = "delete myDt where ID = '" + GridView1.DataKeys[i].Value + "'";
  69.                 sqlComm = new SqlCommand(strSql, sqlConn);
  70.                 sqlConn.Open();
  71.                 sqlComm.ExecuteNonQuery();
  72.                 sqlConn.Close();
  73.             }
  74.         }
  75.         databind();
  76.     }
  77. }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值