GridView实现跨页全选,并收集选中项的主键

HTML代码:

代码
 1 <asp:GridView ID="gvStudent" runat="server" CellPadding="4" ForeColor="#333333" GridLines="None"
 2                                             Width="680px" AllowPaging="True" AutoGenerateColumns="False" OnPageIndexChanging="gvStudent_PageIndexChanging" OnRowDataBound="gvStudent_RowDataBound" PageSize="5">
 3                                             <FooterStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
 4                                             <RowStyle BackColor="#EFF3FB" />
 5                                             <EditRowStyle BackColor="#2461BF" />
 6                                             <SelectedRowStyle BackColor="#D1DDF1" Font-Bold="True" ForeColor="#333333" />
 7                                             <PagerStyle BackColor="#2461BF" ForeColor="White" HorizontalAlign="Center" />
 8                                             <HeaderStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
 9                                             <AlternatingRowStyle BackColor="White" />
10                                             <Columns>
11                                                 <asp:TemplateField>
12                                                     <HeaderTemplate>
13                                                         <asp:CheckBox ID="chkAll" runat="server" AutoPostBack="True" Text="全选" OnCheckedChanged="chkAll_CheckedChanged" />
14                                                     </HeaderTemplate>
15                                                     <ItemStyle HorizontalAlign="Center" />
16                                                     <HeaderStyle HorizontalAlign="Center" />
17                                                     <ItemTemplate>
18                                                         <asp:CheckBox ID="chkOne" runat="server" />
19                                                     </ItemTemplate>
20                                                 </asp:TemplateField>
21                                                 <asp:TemplateField HeaderText="序号">
22                                                     <EditItemTemplate>
23                                                         <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
24                                                     </EditItemTemplate>
25                                                     <ItemStyle HorizontalAlign="Center" />
26                                                     <HeaderStyle HorizontalAlign="Center" />
27                                                     <ItemTemplate>
28                                                         <%# gvStudent.PageSize*gvStudent.PageIndex+gvStudent.Rows.Count+1 %>
29                                                     </ItemTemplate>
30                                                 </asp:TemplateField>
31                                                 <asp:BoundField HeaderText="小学号" DataField="StuNO">
32                                                     <ItemStyle HorizontalAlign="Center" />
33                                                     <HeaderStyle HorizontalAlign="Center" />
34                                                 </asp:BoundField>
35                                                 <asp:BoundField HeaderText="学号" DataField="StuID">
36                                                     <ItemStyle HorizontalAlign="Center" />
37                                                     <HeaderStyle HorizontalAlign="Center" />
38                                                 </asp:BoundField>
39                                                 <asp:BoundField HeaderText="姓名" DataField="XM">
40                                                     <ItemStyle HorizontalAlign="Center" />
41                                                     <HeaderStyle HorizontalAlign="Center" />
42                                                 </asp:BoundField>
43                                                 <asp:BoundField HeaderText="班级" DataField="ClassName">
44                                                     <ItemStyle HorizontalAlign="Center" />
45                                                     <HeaderStyle HorizontalAlign="Center" />
46                                                 </asp:BoundField>
47                                                 <asp:BoundField HeaderText="专业" DataField="MajorName">
48                                                     <ItemStyle HorizontalAlign="Center" />
49                                                     <HeaderStyle HorizontalAlign="Center" />
50                                                 </asp:BoundField>
51                                             </Columns>
52                                         </asp:GridView>
53 <asp:Button ID="btnOK" runat="server" Text="确定" OnClick="btnOK_Click" />

CS代码:

代码
  1 protected void Page_Load(object sender, EventArgs e)
  2     {
  3         if (!IsPostBack)
  4         {
  5             if (ViewState["IDS"== null)
  6             {
  7                 ArrayList IDSNew = new ArrayList();
  8                 ViewState["IDS"= IDSNew;
  9             }
 10         }
 11     }
 12 
 13 private void BindClassMember()
 14     {
 15         DataSet ds = new DataSet();
 16         ds = cm.GetClassMember(ddlGrade.SelectedValue, ddlDept.SelectedValue, ddlMajor.SelectedValue, "-1", ddlClass.SelectedValue);
 17         gvStudent.DataKeyNames = new string[] { "ID" };
 18         gvStudent.DataSource = ds;
 19         gvStudent.DataBind();
 20     }
 21 protected void chkAll_CheckedChanged(object sender, EventArgs e)
 22     {
 23         CheckBox ALL = (CheckBox)gvStudent.HeaderRow.FindControl("chkAll");
 24         foreach (GridViewRow gr in gvStudent.Rows)
 25         {
 26             CheckBox One = (CheckBox)gr.FindControl("chkOne");
 27             One.Checked = ALL.Checked;
 28         }
 29     }
 30     protected void gvStudent_PageIndexChanging(object sender, GridViewPageEventArgs e)
 31     {
 32         ArrayList IDS = (ArrayList)ViewState["IDS"];
 33         gvStudent.PageIndex = e.NewPageIndex;
 34         for (int i = 0; i < gvStudent.Rows.Count; i++)
 35         {
 36             CheckBox One = (CheckBox)gvStudent.Rows[i].FindControl("ChkOne");
 37             int ID = Convert.ToInt32(gvStudent.DataKeys[i].Value);
 38             if (One.Checked)
 39             {
 40                 if (!IDS.Contains(ID))
 41                 {
 42                     IDS.Add(ID);
 43                 }
 44             }
 45             else
 46             {
 47                 if (IDS.Contains(ID))
 48                 {
 49                     IDS.Remove(ID);
 50                 }
 51             }
 52         }
 53         ViewState["IDS"= IDS;
 54         BindClassMember();
 55     }
 56     protected void gvStudent_RowDataBound(object sender, GridViewRowEventArgs e)
 57     {
 58         if (e.Row.RowType == DataControlRowType.DataRow)
 59         {
 60             int ID = Convert.ToInt32(gvStudent.DataKeys[e.Row.RowIndex].Value);
 61             if (ViewState["IDS"!= null)
 62             {
 63                 ArrayList IDS = (ArrayList)ViewState["IDS"];
 64                 if (IDS.Count > 0)
 65                 {
 66                     for (int i = 0; i < IDS.Count; i++)
 67                     {
 68                         if ((int)IDS[i] == ID)
 69                         {
 70                             ((CheckBox)e.Row.FindControl("ChkOne")).Checked = true;
 71                         }
 72                     }
 73                 }
 74             }
 75         }
 76     }
 77     protected void btnOK_Click(object sender, EventArgs e)
 78     {
 79         ArrayList IDS = (ArrayList)ViewState["IDS"];
 80         if (gvStudent.PageIndex == 0||gvStudent.PageIndex==gvStudent.PageCount-1)
 81         {
 82             for (int i = 0; i < gvStudent.Rows.Count; i++)
 83             {
 84                 CheckBox One = (CheckBox)gvStudent.Rows[i].FindControl("ChkOne");
 85                 int ID = Convert.ToInt32(gvStudent.DataKeys[i].Value);
 86                 if (One.Checked)
 87                 {
 88                     if (!IDS.Contains(ID))
 89                     {
 90                         IDS.Add(ID);
 91                     }
 92                 }
 93                 else
 94                 {
 95                     if (IDS.Contains(ID))
 96                     {
 97                         IDS.Remove(ID);
 98                     }
 99                 }
100             }
101             ViewState["IDS"= IDS;
102         }
103     }
原文地址:https://www.cnblogs.com/lavenderzh/p/1702204.html