转载的
前台代码:
代码
1 <%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default3.aspx.cs" Inherits="Default3" %>
2
3 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
4
5 <html xmlns="http://www.w3.org/1999/xhtml">
6 <head runat="server">
7 <title>无标题页</title>
8 <style type="text/css">
9 .style1
10 {
11 width: 100%;
12 }
13 </style>
14 </head>
15 <body>
16 <form id="form1" runat="server">
17 <div style="text-align: center; height: 640px">
18
19 <table class="style1">
20 <tr>
21 <td>
22 <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False"
23 BackColor="White" BorderColor="#E7E7FF" BorderStyle="None" BorderWidth="1px"
24 CellPadding="3" GridLines="Horizontal">
25 <FooterStyle BackColor="#B5C7DE" ForeColor="#4A3C8C" />
26 <RowStyle BackColor="#E7E7FF" ForeColor="#4A3C8C" />
27 <Columns>
28 <asp:TemplateField HeaderText="选择">
29 <ItemTemplate>
30 <asp:CheckBox ID="CheckBox1" runat="server" />
31 </ItemTemplate>
32 </asp:TemplateField>
33 <asp:BoundField DataField="id" HeaderText="编号" />
34 <asp:BoundField DataField="name" HeaderText="姓名" />
35 <asp:BoundField DataField="sex" HeaderText="性别" />
36 <asp:BoundField DataField="department" HeaderText="专业" />
37 <asp:BoundField DataField="grade" HeaderText="年级" />
38 <asp:CommandField ShowDeleteButton="True" />
39 </Columns>
40 <PagerStyle BackColor="#E7E7FF" ForeColor="#4A3C8C" HorizontalAlign="Right" />
41 <SelectedRowStyle BackColor="#738A9C" Font-Bold="True" ForeColor="#F7F7F7" />
42 <HeaderStyle BackColor="#4A3C8C" Font-Bold="True" ForeColor="#F7F7F7" />
43 <AlternatingRowStyle BackColor="#F7F7F7" />
44 </asp:GridView>
45 </td>
46 <td colspan="2">
47 </td>
48 </tr>
49 <tr>
50 <td align="center" >
51 <asp:CheckBox ID="CheckBox2" runat="server" Text="全选"
52 oncheckedchanged="CheckBox2_CheckedChanged" AutoPostBack="True" />
53
54 <asp:Button ID="Button2" runat="server" Text="取消" onclick="Button2_Click" />
55
56 <asp:Button ID="Button3" runat="server" Text="删除" onclick="Button3_Click" />
57 </td>
58 <td >
59 </td>
60 <td>
61 </td>
62 </tr>
63 </table>
64
65 </div>
66 </form>
67 </body>
68 </html>
后台代码:
代码
1 using System;
2 using System.Collections;
3 using System.Configuration;
4 using System.Data;
5 using System.Web;
6 using System.Web.Security;
7 using System.Web.UI;
8 using System.Web.UI.HtmlControls;
9 using System.Web.UI.WebControls;
10 using System.Web.UI.WebControls.WebParts;
11 using System.Data.SqlClient;
12
13 public partial class Default3 : System.Web.UI.Page
14 {
15 protected void Page_Load(object sender, EventArgs e)
16 {
17 if (!IsPostBack)
18 {
19 Bind();
20 }
21 }
22
23 private void Bind()
24 {
25 SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["Personal"].ConnectionString);
26 conn.Open();
27 SqlDataAdapter adp = new SqlDataAdapter("select * from information", conn);
28 DataSet dataset = new DataSet();
29 adp.Fill(dataset, "information");
30 conn.Close();
31 GridView1.DataSource = dataset;
32 GridView1.DataKeyNames = new String[] { "id" };
33 GridView1.DataBind();
34 }
35 protected void CheckBox2_CheckedChanged(object sender, EventArgs e)
36 {
37 for (int i = 0; i <= GridView1.Rows.Count - 1; i++)
38 {
39 CheckBox cbox = (CheckBox)GridView1.Rows[i].FindControl("CheckBox1");
40 if (CheckBox2.Checked == true)
41 {
42 cbox.Checked = true;
43 }
44 else
45 {
46 cbox.Checked = false;
47 }
48 }
49 }
50 protected void Button2_Click(object sender, EventArgs e)
51 {
52 CheckBox2.Checked = false;
53 for (int i = 0; i <= GridView1.Rows.Count - 1; i++)
54 {
55 CheckBox cbox = (CheckBox)GridView1.Rows[i].FindControl("CheckBox1");
56 cbox.Checked = false;
57 }
58 }
59 protected void Button3_Click(object sender, EventArgs e)
60 {
61 for (int i = 0; i <= GridView1.Rows.Count - 1; i++)
62 {
63 CheckBox cbox = (CheckBox)GridView1.Rows[i].FindControl("CheckBox1");
64 if (cbox.Checked == true)
65 {
66 SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["Personal"].ConnectionString);
67 SqlCommand comm = new SqlCommand("delete from information where id='" + GridView1.DataKeys[i].Value + "'", conn);
68 conn.Open();
69 comm.ExecuteNonQuery();
70 conn.Close();
71 }
72 }
73 Bind();
74 }
75 }

浙公网安备 33010602011771号