1、页面代码:
1: <%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>
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: </head>
9: <body>
10: <form id="form1" runat="server">
11: <div>
12: </div>
13: <table class="style1">
14: <tr>
15: <td class="style2">
16: <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False"
17: DataSourceID="SqlDataSource1" Width="762px">
18: <Columns>
19: <asp:TemplateField>
20: <ItemTemplate>
21: <asp:CheckBox ID="CheckBox1" runat="server" />
22: </ItemTemplate>
23: </asp:TemplateField>
24: <asp:BoundField DataField="id" HeaderText="id" SortExpression="id" />
25: <asp:BoundField DataField="name" HeaderText="name" SortExpression="name" />
26: <asp:BoundField DataField="sex" HeaderText="sex" SortExpression="sex" />
27: <asp:BoundField DataField="nPlace" HeaderText="nPlace"
28: SortExpression="nPlace" />
29: <asp:BoundField DataField="date" HeaderText="date" SortExpression="date" />
30: <asp:BoundField DataField="money" HeaderText="money" SortExpression="money" />
31: <asp:BoundField DataField="photo" HeaderText="photo" SortExpression="photo" />
32: </Columns>
33: </asp:GridView>
34: </td>
35: <td>
36: </td>
37: </tr>
38: <tr>
39: <td class="style2">
40: <asp:CheckBox ID="chkSeletAll" runat="server" Text="全选" AutoPostBack="True"
41: oncheckedchanged="chkSeletAll_CheckedChanged" />
42: <asp:Button ID="btnCancel" runat="server" Text="取消" onclick="btnCancel_Click" />
43: <asp:Button ID="btnDelete" runat="server" Text="删除" />
44: </td>
45: <td>
46: </td>
47: </tr>
48: </table>
49: <asp:SqlDataSource ID="SqlDataSource1" runat="server"
50: ConnectionString="<%$ ConnectionStrings:db_04ConnectionString %>"
51: SelectCommand="SELECT * FROM [tb_Member]"></asp:SqlDataSource>
52: </form>
53: </body>
54: </html>
2、执行代码:
1: using System;
2: using System.Web.UI.WebControls;
3:
4: public partial class _Default : System.Web.UI.Page
5: {
6: protected void Page_Load(object sender, EventArgs e)
7: {
8:
9: }
10: /// <summary>
11: /// Handles the CheckedChanged event of the chkSeletAll control.
12: /// </summary>
13: /// <param name="sender">The source of the event.</param>
14: /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
15: protected void chkSeletAll_CheckedChanged(object sender, EventArgs e)
16: {
17: for (int i = 0; i <= GridView1.Rows.Count - 1; i++)
18: {
19: CheckBox cbox = (CheckBox)GridView1.Rows[i].FindControl("CheckBox1");
20: if (chkSeletAll.Checked == true)
21: {
22: cbox.Checked = true;
23: }
24: else
25: {
26: cbox.Checked = false;
27: }
28: }
29: }
30: /// <summary>
31: /// Handles the Click event of the btnCancel control.
32: /// </summary>
33: /// <param name="sender">The source of the event.</param>
34: /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
35: protected void btnCancel_Click(object sender, EventArgs e)
36: {
37: chkSeletAll.Checked = false;
38: for (int i = 0; i <= GridView1.Rows.Count - 1; i++)
39: {
40: CheckBox cbox = (CheckBox)GridView1.Rows[i].FindControl("CheckBox1");
41: cbox.Checked = false;
42: }
43: }
44: }