GridView加入CheckBox实现全选,跨页CheckBox选择

时间:2022-01-04 09:32:17

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="GridView_CheckBoxColumn.aspx.cs" Inherits="GridSamples_GridView_CheckBoxColumn" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
       <title>无标题页</title>
       <script language="javascript" type="text/javascript">
       function selectAll(obj)
       {
           var theTable     = obj.parentElement.parentElement.parentElement;
           var i;
           var j = obj.parentElement.cellIndex;
        
           for(i=0;i<theTable.rows.length;i++)
           {
               var objCheckBox = theTable.rows[i].cells[j].firstChild;
               if(objCheckBox.checked!=null)objCheckBox.checked = obj.checked;
           }
       }
       </script>
</head>
<body>
       <form id="form1" runat="server">
       <div>
           <asp:GridView ID="GridView1" runat="server" AllowPaging="True" AutoGenerateColumns="False"
               DataKeyNames="id" DataSourceID="AccessDataSource1" AllowSorting="True" OnDataBinding="GridView1_DataBinding" OnRowDataBound="GridView1_RowDataBound">
               <Columns>
                   <asp:TemplateField>
                       <ItemTemplate>
                           <asp:CheckBox ID="CheckBox1" runat="server" Checked="True" Text='<%#DataBinder.Eval(Container.DataItem,"id") %>' />
                       </ItemTemplate>
                       <HeaderTemplate>
                           &nbsp;<input id="CheckAll" type="checkbox" onclick="selectAll(this);" />本页全选
                       </HeaderTemplate>
                   </asp:TemplateField>
                   <asp:BoundField DataField="id" HeaderText="id" InsertVisible="False" ReadOnly="True"
                       SortExpression="id" />
                   <asp:BoundField DataField="name" HeaderText="name" SortExpression="name" />
                   <asp:BoundField DataField="sex" HeaderText="sex" SortExpression="sex" />
                   <asp:BoundField DataField="deptid" HeaderText="deptid" SortExpression="deptid" />
               </Columns>
           </asp:GridView>
           &nbsp;
      
           <asp:AccessDataSource ID="AccessDataSource1" runat="server" DataFile="~/App_Data/test.mdb"
               SelectCommand="SELECT [id], [name], [sex], [deptid] FROM [employees]"></asp:AccessDataSource>
           <asp:Button ID="Button1" runat="server" OnClick="Button1_Click" Text="ShowAllSelectedItem" />
           <asp:TextBox ID="TextBox1" runat="server" Width="200px" ReadOnly="True"></asp:TextBox></div>
       </form>
</body>
</html>

************************************
************************************
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

public partial class GridSamples_GridView_CheckBoxColumn : System.Web.UI.Page
{
       /// <summary>
       /// 获取或设置选中项的集合
       /// </summary>
       protected ArrayList SelectedItems
       {
           get
           {
               return (ViewState["mySelectedItems"] != null) ? (ArrayList)ViewState["mySelectedItems"] : null;
           }
           set
           {
               ViewState["mySelectedItems"] = value;
           }
       }

       protected void Page_Load(object sender, EventArgs e)
       {
        
       }


       protected void GridView1_DataBinding(object sender, EventArgs e)
       {
           //在每一次重新绑定之前,需要调用CollectSelected方法从当前页收集选中项的情况
           CollectSelected();
       }

    
       protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
       {
           //这里的处理是为了回显之前选中的情况
           if (e.Row.RowIndex > -1 && this.SelectedItems!=null)
           {
               DataRowView row = e.Row.DataItem as DataRowView;
               CheckBox cb = e.Row.FindControl("CheckBox1") as CheckBox;
               if(this.SelectedItems.Contains(row["id"].ToString()))
                   cb.Checked = true;
               else
                   cb.Checked = false;
           }
       }
       /// <summary>
       /// 从当前页收集选中项的情况
       /// </summary>
       protected void CollectSelected()
       {
           ArrayList selectedItems = null;
           if (this.SelectedItems == null)
               selectedItems = new ArrayList();
           else
               selectedItems = this.SelectedItems;

           for (int i = 0; i < this.GridView1.Rows.Count; i++)
           {
               string id = this.GridView1.Rows[i].Cells[1].Text;
               CheckBox cb = this.GridView1.Rows[i].FindControl("CheckBox1") as CheckBox;
               if (selectedItems.Contains(id) && !cb.Checked)
                   selectedItems.Remove(id);
               if (!selectedItems.Contains(id) && cb.Checked)
                   selectedItems.Add(id);
           }
           this.SelectedItems = selectedItems;
       }

       protected void Button1_Click(object sender, EventArgs e)
       {
           //最后,需要对选中项进行操作之前,不能忘了还要最后一次收集当前页的选中情况
           CollectSelected();

           this.TextBox1.Text = string.Empty;
           foreach (object tmp in this.SelectedItems)
               this.TextBox1.Text += tmp.ToString() + ",";
       }