首先下载:AspNetPager.dll AspNetPager.xml 放到bin目录下
页面添加<%@ Register Assembly="AspNetPager" Namespace="Wuqi.Webdiyer" TagPrefix="webdiyer" %>
1 <webdiyer:AspNetPager ID="AspNetPager1" runat="server" OnPageChanged="AspNetPager1_PageChanged"
2 FirstPageText="首页" LastPageText="尾页" NextPageText="下一页" PrevPageText="上一页" ShowPageIndexBox="Never"
3 AlwaysShow="true" UrlPaging="True" ReverseUrlPageIndex="True">
4 </webdiyer:AspNetPager>
cs代码中:
1 DataTable dt = new DataTable();
2 PagedDataSource pds = new PagedDataSource();
3
4 protected void Page_Load(object sender, EventArgs e)
5 {
6 dt = .......;
7 pds.DataSource = dt.DefaultView;
8 pds.AllowPaging = true;
9 pds.PageSize = 10;
10 AspNetPager1.RecordCount = pds.Count;
11 AspNetPager1.PageSize = pds.PageSize;
12 if (!IsPostBack)
13 {
14 Repeater1.DataSource = pds;
15 Repeater1.DataBind();
16 }
17 }
18 }
19
20 protected void AspNetPager1_PageChanged(object sender, EventArgs e)
21 {
22 pds.CurrentPageIndex = AspNetPager1.CurrentPageIndex - 1;//索引同步
23 Repeater1.DataSource = pds;//数据绑定
24 Repeater1.DataBind();
25 }
这样分页显示没问题了,但要是页面中有检索条件,需要重新给datatable赋值的话,点翻页就会出问题了。比如检索出的数据有5页,而page_load中加载的datatable有10页。
点击检索按钮检索出需要的数据后,再翻页又变成全部数据了。下边解决。
DataTable dt = new DataTable();
PagedDataSource pds = new PagedDataSource();
static DataView view = new DataView();
protected void Page_Load(object sender, EventArgs e)
{
dt = .....;
if (Request.QueryString["page"] == null)
{
view = dt.DefaultView;
}
if (!IsPostBack)
{
pds.DataSource = view;
AspNetPager1.RecordCount = view.Count;
pds.AllowPaging = true;
pds.PageSize = 5;
AspNetPager1.PageSize = pds.PageSize;
Repeater1.DataSource = pds;
Repeater1.DataBind();
}
}
protected void AspNetPager1_PageChanged(object sender, EventArgs e)
{
pds.CurrentPageIndex = AspNetPager1.CurrentPageIndex - 1;//索引同步
Repeater1.DataSource = pds;//数据绑定
Repeater1.DataBind();
}
//按条件检索
protected void Button1_Click(object sender, EventArgs e)
{
//button1 根据订单编号和订单时间搜索
string ddh = TextBox1.Text.Replace(" ", "");
string startDt = TxtStartTime.Value;
string endDt = TxtEndTime.Value;
dt = 新table;
view = dt.DefaultView;
pds.DataSource = view;
AspNetPager1.RecordCount = view.Count;
pds.AllowPaging = true;
pds.PageSize = 5;
AspNetPager1.PageSize = pds.PageSize;
Repeater1.DataSource = pds;
Repeater1.DataBind();
}