我写的PageDataSurce分页的
PageDataSurce分页因为Http无状态的特性,所以无法保存pagedatasource状态值,所以每次请求都必须重新来指定CurrentPage的值...
一般是用QueryString来传递当前页的值。。。
我这个没用这种方法,我的方法是把PageDataSource保存在Cache中,所以就一直保存了它的装态。不需要再用QueryString传递了....
后台代码:
一般是用QueryString来传递当前页的值。。。
我这个没用这种方法,我的方法是把PageDataSource保存在Cache中,所以就一直保存了它的装态。不需要再用QueryString传递了....
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 <table>
13 <asp:Repeater ID="Repeater1" runat="server">
14 <ItemTemplate>
15
16 <tr>
17 <td><%#DataBinder.Eval(Container.DataItem,"Name")%></td>
18 <td><%#DataBinder.Eval(Container.DataItem,"Tel")%></td>
19 <td><%#DataBinder.Eval(Container.DataItem,"Description")%></td>
20 </tr>
21 </ItemTemplate>
22 </asp:Repeater>
23 </table>
24 <asp:Button ID="Button1" runat="server" CommandArgument="1" CommandName="Btn" OnClick="Btn_OnClick" Text="首页" />
25 <asp:Button ID="Button2" runat="server" CommandArgument="2" CommandName="Btn" OnClick="Btn_OnClick" Text="上一页" />
26 <asp:Button ID="Button3" runat="server" CommandArgument="3" CommandName="Btn" OnClick="Btn_OnClick" Text="下一页" />
27 <asp:Button ID="Button4" runat="server" CommandArgument="4" CommandName="Btn" OnClick="Btn_OnClick" Text="末页" />
28 </div>
29 </form>
30</body>
31</html>
32
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 <table>
13 <asp:Repeater ID="Repeater1" runat="server">
14 <ItemTemplate>
15
16 <tr>
17 <td><%#DataBinder.Eval(Container.DataItem,"Name")%></td>
18 <td><%#DataBinder.Eval(Container.DataItem,"Tel")%></td>
19 <td><%#DataBinder.Eval(Container.DataItem,"Description")%></td>
20 </tr>
21 </ItemTemplate>
22 </asp:Repeater>
23 </table>
24 <asp:Button ID="Button1" runat="server" CommandArgument="1" CommandName="Btn" OnClick="Btn_OnClick" Text="首页" />
25 <asp:Button ID="Button2" runat="server" CommandArgument="2" CommandName="Btn" OnClick="Btn_OnClick" Text="上一页" />
26 <asp:Button ID="Button3" runat="server" CommandArgument="3" CommandName="Btn" OnClick="Btn_OnClick" Text="下一页" />
27 <asp:Button ID="Button4" runat="server" CommandArgument="4" CommandName="Btn" OnClick="Btn_OnClick" Text="末页" />
28 </div>
29 </form>
30</body>
31</html>
32
后台代码:
using System;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Data.SqlClient;
public partial class _Default : System.Web.UI.Page
{
private PagedDataSource pd;
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
DataTable dt = new DataTable();
dt.Columns.Add("ID", Type.GetType("System.String"));
dt.Columns.Add("Name", Type.GetType("System.String"));
dt.Columns.Add("Tel", Type.GetType("System.String"));
dt.Columns.Add("Description", Type.GetType("System.String"));
dt.Rows.Add("1", "ddd", "123456", "个人人人人");
dt.Rows.Add("2", "ddd", "56565656", "提供方多个");
dt.Rows.Add("3", "qqq", "565656", "大师父是非得失");
dt.Rows.Add("4", "www", "788888", "上的发生地方上的");
dt.Rows.Add("5", "qas", "33333", "双方上的发生");
dt.Rows.Add("6", "fff", "222222", "但是发的十分上的");
dt.Rows.Add("7", "ccc", "33333", "双方都是非");
InitalDataSourece(dt);
}
}
private void InitalDataSourece(DataTable dt)
{
pd = new PagedDataSource();
pd.DataSource = dt.DefaultView ;
pd.PageSize = 2;
pd.AllowPaging = true;
Cache["Pd"] = pd;
DataBind();
}
private void DataBind()
{
Repeater1.DataSource = pd;
Repeater1.DataBind();
}
protected void Btn_OnClick(object sender, EventArgs e)
{
Button btn = (Button)sender;
pd = (PagedDataSource)Cache["Pd"];
if (btn.CommandArgument == "1")
{
pd.CurrentPageIndex = 0;
}
else if (btn.CommandArgument == "2")
{
if (pd.IsFirstPage)
{
pd.CurrentPageIndex = 0;
}
else
{
pd.CurrentPageIndex -= 1;
};
}
else if (btn.CommandArgument == "3")
{
if (pd.IsLastPage)
{
pd.CurrentPageIndex = pd.PageCount - 1;
}
else
{
pd.CurrentPageIndex += 1;
}
}
else
{
pd.CurrentPageIndex = pd.PageCount - 1;
}
DataBind();
}
}
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Data.SqlClient;
public partial class _Default : System.Web.UI.Page
{
private PagedDataSource pd;
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
DataTable dt = new DataTable();
dt.Columns.Add("ID", Type.GetType("System.String"));
dt.Columns.Add("Name", Type.GetType("System.String"));
dt.Columns.Add("Tel", Type.GetType("System.String"));
dt.Columns.Add("Description", Type.GetType("System.String"));
dt.Rows.Add("1", "ddd", "123456", "个人人人人");
dt.Rows.Add("2", "ddd", "56565656", "提供方多个");
dt.Rows.Add("3", "qqq", "565656", "大师父是非得失");
dt.Rows.Add("4", "www", "788888", "上的发生地方上的");
dt.Rows.Add("5", "qas", "33333", "双方上的发生");
dt.Rows.Add("6", "fff", "222222", "但是发的十分上的");
dt.Rows.Add("7", "ccc", "33333", "双方都是非");
InitalDataSourece(dt);
}
}
private void InitalDataSourece(DataTable dt)
{
pd = new PagedDataSource();
pd.DataSource = dt.DefaultView ;
pd.PageSize = 2;
pd.AllowPaging = true;
Cache["Pd"] = pd;
DataBind();
}
private void DataBind()
{
Repeater1.DataSource = pd;
Repeater1.DataBind();
}
protected void Btn_OnClick(object sender, EventArgs e)
{
Button btn = (Button)sender;
pd = (PagedDataSource)Cache["Pd"];
if (btn.CommandArgument == "1")
{
pd.CurrentPageIndex = 0;
}
else if (btn.CommandArgument == "2")
{
if (pd.IsFirstPage)
{
pd.CurrentPageIndex = 0;
}
else
{
pd.CurrentPageIndex -= 1;
};
}
else if (btn.CommandArgument == "3")
{
if (pd.IsLastPage)
{
pd.CurrentPageIndex = pd.PageCount - 1;
}
else
{
pd.CurrentPageIndex += 1;
}
}
else
{
pd.CurrentPageIndex = pd.PageCount - 1;
}
DataBind();
}
}