首页 > 代码库 > .net分页控件webdiyer:AspNetPager

.net分页控件webdiyer:AspNetPager

首先下载: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页。

点击检索按钮检索出需要的数据后,再翻页又变成全部数据了。下边解决。

 1 DataTable dt = new DataTable();
 2     PagedDataSource pds = new PagedDataSource();
 3     static DataView view = new DataView();
 4     protected void Page_Load(object sender, EventArgs e)
 5     {
 6         dt = .....;
 7         if (Request.QueryString["page"] == null)
 8         {
 9             view = dt.DefaultView;                   
10         }
11         if (!IsPostBack)
12         {
13             pds.DataSource = view;
14             AspNetPager1.RecordCount = view.Count;
15             pds.AllowPaging = true;
16             pds.PageSize = 5;
17             AspNetPager1.PageSize = pds.PageSize;
18             Repeater1.DataSource = pds;
19             Repeater1.DataBind();
20         }
21     }
22     protected void AspNetPager1_PageChanged(object sender, EventArgs e)
23     {
24         pds.CurrentPageIndex = AspNetPager1.CurrentPageIndex - 1;//索引同步
25         Repeater1.DataSource = pds;//数据绑定
26         Repeater1.DataBind();
27     }
28     //按条件检索
29     protected void Button1_Click(object sender, EventArgs e)
30     {
31         //button1  根据订单编号和订单时间搜索
32         string ddh = TextBox1.Text.Replace(" ", "");
33         string startDt = TxtStartTime.Value;
34         string endDt = TxtEndTime.Value;
35         dt = 新table;
36         view = dt.DefaultView;
37         pds.DataSource = view;
38         AspNetPager1.RecordCount = view.Count;
39         pds.AllowPaging = true;
40         pds.PageSize = 5;
41         AspNetPager1.PageSize = pds.PageSize;
42         Repeater1.DataSource = pds;
43         Repeater1.DataBind();
44     }

 

.net分页控件webdiyer:AspNetPager