网上很多保持分页的datagird的checkbox选择状态的文章实现的是保存当前页面的chexkbox所选,也就是说第一页选择了第一条记录翻页到第二页后还是选择第一条记录,然后选择了第二条记录,再去看第一页还是第二条记录,保存的只是上次操作的结果,而我们往往希望分开保存所有页面的选择情况,下面是示例代码:
前台:
<%@ Page language="c#" Codebehind="WebForm1.aspx.cs" AutoEventWireup="false" Inherits="checktest.WebForm1" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" >
<HTML>
<HEAD>
<title>WebForm1</title>
<meta name="GENERATOR" Content="Microsoft Visual Studio .NET 7.1">
<meta name="CODE_LANGUAGE" Content="C#">
<meta name="vs_defaultClientScript" content="JavaScript">
<meta name="vs_targetSchema" content="http://schemas.microsoft.com/intellisense/ie5">
<LINK href="css.css" type="text/css" rel="stylesheet">
</HEAD>
<body>
<form id="Form1" method="post" runat="server">
<asp:datagrid id="DataGrid1" runat="server" AutoGenerateColumns="False" CellSpacing="1" BorderWidth="0px"
CellPadding="5" CssClass="border" AllowPaging="True" PageSize="10">
<ItemStyle CssClass="item"></ItemStyle>
<HeaderStyle CssClass="header"></HeaderStyle>
<Columns>
<asp:TemplateColumn>
<ItemTemplate>
<asp:CheckBox ID="chk" Runat="server"></asp:CheckBox>
</ItemTemplate>
</asp:TemplateColumn>
<asp:BoundColumn DataField="CustomerID" HeaderText="CustomerID"></asp:BoundColumn>
<asp:BoundColumn DataField="CompanyName" HeaderText="CompanyName"></asp:BoundColumn>
<asp:BoundColumn DataField="ContactTitle" HeaderText="ContactTitle"></asp:BoundColumn>
</Columns>
<PagerStyle CssClass="header" Mode="NumericPages"></PagerStyle>
</asp:datagrid>
<asp:Button id="Button1" runat="server" Text="清空记录"></asp:Button>
</form>
</body>
</HTML>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" >
<HTML>
<HEAD>
<title>WebForm1</title>
<meta name="GENERATOR" Content="Microsoft Visual Studio .NET 7.1">
<meta name="CODE_LANGUAGE" Content="C#">
<meta name="vs_defaultClientScript" content="JavaScript">
<meta name="vs_targetSchema" content="http://schemas.microsoft.com/intellisense/ie5">
<LINK href="css.css" type="text/css" rel="stylesheet">
</HEAD>
<body>
<form id="Form1" method="post" runat="server">
<asp:datagrid id="DataGrid1" runat="server" AutoGenerateColumns="False" CellSpacing="1" BorderWidth="0px"
CellPadding="5" CssClass="border" AllowPaging="True" PageSize="10">
<ItemStyle CssClass="item"></ItemStyle>
<HeaderStyle CssClass="header"></HeaderStyle>
<Columns>
<asp:TemplateColumn>
<ItemTemplate>
<asp:CheckBox ID="chk" Runat="server"></asp:CheckBox>
</ItemTemplate>
</asp:TemplateColumn>
<asp:BoundColumn DataField="CustomerID" HeaderText="CustomerID"></asp:BoundColumn>
<asp:BoundColumn DataField="CompanyName" HeaderText="CompanyName"></asp:BoundColumn>
<asp:BoundColumn DataField="ContactTitle" HeaderText="ContactTitle"></asp:BoundColumn>
</Columns>
<PagerStyle CssClass="header" Mode="NumericPages"></PagerStyle>
</asp:datagrid>
<asp:Button id="Button1" runat="server" Text="清空记录"></asp:Button>
</form>
</body>
</HTML>
后台:
using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
namespace checktest
{
public class WebForm1 : System.Web.UI.Page
{
protected System.Web.UI.WebControls.Button Button1;
protected System.Web.UI.WebControls.DataGrid DataGrid1;
private void Page_Load(object sender, System.EventArgs e)
{
if(!IsPostBack)
{
SetBind();
}
}
private void SetBind()
{
SqlConnection conn=new SqlConnection("server=(local);uid=sa;pwd=sa
;database=Northwind");
SqlDataAdapter da=new SqlDataAdapter("select * from Customers",conn);
DataSet ds=new DataSet();
da.Fill(ds,"table1");
this.DataGrid1.DataSource=ds.Tables["table1"];
this.DataGrid1.DataBind();
}
#region Web 窗体设计器生成的代码
override protected void OnInit(EventArgs e)
{
//
// CODEGEN: 该调用是 ASP.NET Web 窗体设计器所必需的。
//
InitializeComponent();
base.OnInit(e);
}
/// <summary>
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
/// 此方法的内容。
/// </summary>
private void InitializeComponent()
{
this.DataGrid1.PageIndexChanged += new System.Web.UI.WebControls.DataGridPageChangedEventHandler(this.DataGrid1_PageIndexChanged);
this.DataGrid1.ItemDataBound += new System.Web.UI.WebControls.DataGridItemEventHandler(this.DataGrid1_ItemDataBound);
this.Button1.Click += new System.EventHandler(this.Button1_Click);
this.Load += new System.EventHandler(this.Page_Load);
}
#endregion
private void DataGrid1_PageIndexChanged(object source, System.Web.UI.WebControls.DataGridPageChangedEventArgs e)
{
string data="";
foreach(DataGridItem dgi in this.DataGrid1.Items)
{
CheckBox cb=(CheckBox)dgi.FindControl("chk");
if(cb.Checked)
data+="1";
else
data+="0";
}
if(ViewState["pagedata"]!=null)
{
Hashtable ht=(Hashtable)ViewState["pagedata"];
if(ht.Contains(this.DataGrid1.CurrentPageIndex))
ht[this.DataGrid1.CurrentPageIndex]=data;
else
ht.Add(this.DataGrid1.CurrentPageIndex,data);
ViewState["pagedata"]=ht;
}
else
{
Hashtable ht=new Hashtable();
ht.Add(this.DataGrid1.CurrentPageIndex,data);
ViewState["pagedata"]=ht;
}
this.DataGrid1.CurrentPageIndex=e.NewPageIndex;
SetBind();
}
private void DataGrid1_ItemDataBound(object sender, System.Web.UI.WebControls.DataGridItemEventArgs e)
{
if(e.Item.ItemType==ListItemType.Item||e.Item.ItemType==ListItemType.AlternatingItem)
{
if(ViewState["pagedata"]!=null)
{
Hashtable ht=(Hashtable)ViewState["pagedata"];
if(ht.Contains(this.DataGrid1.CurrentPageIndex))
{
CheckBox cb=(CheckBox)e.Item.FindControl("chk");
cb.Checked=ht[this.DataGrid1.CurrentPageIndex].ToString()[e.Item.ItemIndex].ToString()=="1";
}
}
}
}
private void Button1_Click(object sender, System.EventArgs e)
{
if(ViewState["pagedata"]!=null)
{
Hashtable ht=new Hashtable();
ViewState["pagedata"]=ht;
SetBind();
}
}
}
}
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
namespace checktest
{
public class WebForm1 : System.Web.UI.Page
{
protected System.Web.UI.WebControls.Button Button1;
protected System.Web.UI.WebControls.DataGrid DataGrid1;
private void Page_Load(object sender, System.EventArgs e)
{
if(!IsPostBack)
{
SetBind();
}
}
private void SetBind()
{
SqlConnection conn=new SqlConnection("server=(local);uid=sa;pwd=sa
;database=Northwind");
SqlDataAdapter da=new SqlDataAdapter("select * from Customers",conn);
DataSet ds=new DataSet();
da.Fill(ds,"table1");
this.DataGrid1.DataSource=ds.Tables["table1"];
this.DataGrid1.DataBind();
}
#region Web 窗体设计器生成的代码
override protected void OnInit(EventArgs e)
{
//
// CODEGEN: 该调用是 ASP.NET Web 窗体设计器所必需的。
//
InitializeComponent();
base.OnInit(e);
}
/// <summary>
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
/// 此方法的内容。
/// </summary>
private void InitializeComponent()
{
this.DataGrid1.PageIndexChanged += new System.Web.UI.WebControls.DataGridPageChangedEventHandler(this.DataGrid1_PageIndexChanged);
this.DataGrid1.ItemDataBound += new System.Web.UI.WebControls.DataGridItemEventHandler(this.DataGrid1_ItemDataBound);
this.Button1.Click += new System.EventHandler(this.Button1_Click);
this.Load += new System.EventHandler(this.Page_Load);
}
#endregion
private void DataGrid1_PageIndexChanged(object source, System.Web.UI.WebControls.DataGridPageChangedEventArgs e)
{
string data="";
foreach(DataGridItem dgi in this.DataGrid1.Items)
{
CheckBox cb=(CheckBox)dgi.FindControl("chk");
if(cb.Checked)
data+="1";
else
data+="0";
}
if(ViewState["pagedata"]!=null)
{
Hashtable ht=(Hashtable)ViewState["pagedata"];
if(ht.Contains(this.DataGrid1.CurrentPageIndex))
ht[this.DataGrid1.CurrentPageIndex]=data;
else
ht.Add(this.DataGrid1.CurrentPageIndex,data);
ViewState["pagedata"]=ht;
}
else
{
Hashtable ht=new Hashtable();
ht.Add(this.DataGrid1.CurrentPageIndex,data);
ViewState["pagedata"]=ht;
}
this.DataGrid1.CurrentPageIndex=e.NewPageIndex;
SetBind();
}
private void DataGrid1_ItemDataBound(object sender, System.Web.UI.WebControls.DataGridItemEventArgs e)
{
if(e.Item.ItemType==ListItemType.Item||e.Item.ItemType==ListItemType.AlternatingItem)
{
if(ViewState["pagedata"]!=null)
{
Hashtable ht=(Hashtable)ViewState["pagedata"];
if(ht.Contains(this.DataGrid1.CurrentPageIndex))
{
CheckBox cb=(CheckBox)e.Item.FindControl("chk");
cb.Checked=ht[this.DataGrid1.CurrentPageIndex].ToString()[e.Item.ItemIndex].ToString()=="1";
}
}
}
}
private void Button1_Click(object sender, System.EventArgs e)
{
if(ViewState["pagedata"]!=null)
{
Hashtable ht=new Hashtable();
ViewState["pagedata"]=ht;
SetBind();
}
}
}
}
这样的话达到这样的效果,比如分页一个页面10个记录,
我们在第一页的时候选择了1,4,6记录,翻页到第二页
在第二页的时候选择了2,3,7记录,然后翻页回第一页
看到checkbox还是选择的是1,4,6记录,翻页到第二页选择2,3,7,每个页面独立保存自己的状态