Simple Numeric Pagination for DataList and Repeater
来源:互联网 发布:网络牛牛赌博作弊吗 编辑:程序博客网 时间:2024/06/06 11:40
Simple Numeric Pagination for DataList and Repeater
There are a big number of tutorials available on how to implementpagination in asp.net DataList and Repeater. But all of them areexplaining only about the Next/Prev mode of pagination only. Recently Ihave implemented a quick and dirty numeric pagination on DataList forone of my project.
Theidea is simply use a PagedDataSource to get the current page of dataand bind it to DataList or Repeater. Now create dynamic Linkbuttonsusing the PageSize and the Total rows count. See the code below.
ASPX Code
- <head runat="server">
- <title>Untitled Page</title>
- <style type="text/css">
- .pagination a{padding:5px;}
- .pagination span{padding:5px;}
- </style>
- </head>
- <body>
- <form id="form1" runat="server">
- <div>
- <asp:DataList ID="dlPaginationSample" runat="server">
- <ItemTemplate>
- <%# Eval("Column1")%> | <%# Eval("Column2")%>
- </ItemTemplate>
- </asp:DataList>
- </div>
- <asp:Panel runat="server" id="pnlPager" CssClass="pagination">
- </asp:Panel>
- </form>
- </body>
C# Code
- //you can pass either DatList or Repeater to this function
- private void bindDataWithPaging(Control bindControl, DataSet data)
- {
- if (data.Tables.Count > 0) // if the datset contains data
- {
- DataView dv = data.Tables[0].DefaultView;
- PagedDataSource dsP = new PagedDataSource();
- dsP.AllowPaging = true;
- dsP.DataSource = dv;
- dsP.CurrentPageIndex = CurrentPageIndex;
- dsP.PageSize = PageSize;
- //Binding data to the controls
- if (bindControl is DataList)
- {
- ((DataList)bindControl).DataSource= dsP;
- ((DataList)bindControl).DataBind();
- }
- else if (bindControl is Repeater)
- {
- ((Repeater)bindControl).DataSource = dsP;
- ((Repeater)bindControl).DataBind();
- }
- //saving the total page count in Viewstate for later use
- PageCount = dsP.PageCount;
- //create the linkbuttons for pagination
- BuildPagination();
- }
- }
Theabove function is straight forward and self explanatory. What it willdo is simply create a PagedDataSource from the full dataset and bind itto the custom pagination repeater or datalist.
- //create the linkbuttons for pagination
- protected void BuildPagination()
- {
- pnlPager.Controls.Clear(); //
- if (PageCount <= 1) return; // at least two pages should be there to show the pagination
- //finding the first linkbutton to be shown in the current display
- int start = CurrentPageIndex - (CurrentPageIndex % ButtonsCount);
- //finding the last linkbutton to be shown in the current display
- int end = CurrentPageIndex + (ButtonsCount - (CurrentPageIndex % ButtonsCount));
- //if the start button is more than the number of buttons. If the start button is 11 we have to show the <<First link
- if (start > ButtonsCount - 1)
- {
- pnlPager.Controls.Add(createButton(FirstPageText, 0));
- pnlPager.Controls.Add(createButton("..", start - 1));
- }
- int i = 0, j = 0;
- for (i = start; i < end; i++)
- {
- LinkButton lnk;
- if (i < PageCount)
- {
- if (i == CurrentPageIndex) //if its the current page
- {
- Label lbl = new Label();
- lbl.Text = (i + 1).ToString();
- pnlPager.Controls.Add(lbl);
- }
- else
- {
- pnlPager.Controls.Add(createButton((i + 1).ToString(), i));
- }
- }
- j++;
- }
- //If the total number of pages are greaer than the end page we have to show Last>> link
- if (PageCount > end)
- {
- pnlPager.Controls.Add(createButton("..", i));
- pnlPager.Controls.Add(createButton(">>", PageCount - 1));
- }
- }
Thisis the whole logic for a simple implementation. I am using this custompagination in a number of projects and they are working great.
- Simple Numeric Pagination for DataList and Repeater
- Simple Paging in Repeater and DataList Controls
- Simple Numeric Edit Control
- (OK) Table component with sorting and pagination for Angular2
- 对比DataGrid,DataList,Repeater
- datagrid,datalist,Repeater
- 对比DataGrid,DataList,Repeater
- Datalist和Repeater嵌套
- Datalist嵌套Repeater显示
- repeater/DataList分页
- GridView 、DataList 、Repeater 比较
- DataGrid,datalist ,repeater
- Datalist Repeater分页
- Datalist Repeater分页
- Datalist Repeater手写分页
- 不用repeater datalist 打印数据 直接从后台直接打印 一个for 循环打印表格两列多行
- Useful and Simple srcipts for Oracle CoolBackup
- Useful and Simple srcipts for Oracle CoolBackup
- 我的CSDN博客
- jxl实用例子及实现批量转移数据的想法
- Singleton Factory
- 成年人必看的六个故事教你学会做人
- 完整语种代码列表
- Simple Numeric Pagination for DataList and Repeater
- spoonwep破解方式使用心得
- 什么是嵌入式开发
- 英特尔遭遇CPU级RootKit,目前无药可医
- LVS学习笔记(一)
- 回想起小学时
- 关于hibernate的连接池问题
- NULL在SQLServer数据库日志文件中的存储
- 常用汇编器ML.EXE参数解释: