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

view plaincopy to clipboardprint?
  1. <head runat="server">  
  2. <title>Untitled Page</title>  
  3. <style type="text/css">  
  4.     .pagination a{padding:5px;}  
  5.     .pagination span{padding:5px;}  
  6. </style>  
  7. </head>  
  8. <body>  
  9. <form id="form1" runat="server">  
  10.     <div>  
  11.         <asp:DataList ID="dlPaginationSample" runat="server">  
  12.             <ItemTemplate>  
  13.                 <%# Eval("Column1")%> | <%# Eval("Column2")%>  
  14.                   
  15.   
  16.             </ItemTemplate>  
  17.         </asp:DataList>  
  18.     </div>  
  19.   
  20.       
  21.   
  22.     <asp:Panel runat="server" id="pnlPager" CssClass="pagination">  
  23.      
  24.     </asp:Panel>  
  25. </form>  
  26. </body>  


C# Code

view plaincopy to clipboardprint?
  1. //you can pass either DatList or Repeater to this function  
  2. private void bindDataWithPaging(Control bindControl, DataSet data)  
  3. {  
  4.     if (data.Tables.Count > 0) // if the datset contains data  
  5.     {  
  6.         DataView dv = data.Tables[0].DefaultView;  
  7.   
  8.         PagedDataSource dsP = new PagedDataSource();  
  9.         dsP.AllowPaging = true;  
  10.         dsP.DataSource = dv;  
  11.         dsP.CurrentPageIndex = CurrentPageIndex;  
  12.         dsP.PageSize = PageSize;  
  13.   
  14.         //Binding data to the controls  
  15.         if (bindControl is DataList)  
  16.         {  
  17.             ((DataList)bindControl).DataSource= dsP;  
  18.             ((DataList)bindControl).DataBind();  
  19.         }  
  20.         else if (bindControl is Repeater)  
  21.         {  
  22.             ((Repeater)bindControl).DataSource = dsP;  
  23.             ((Repeater)bindControl).DataBind();  
  24.         }  
  25.   
  26.         //saving the total page count in Viewstate for later use  
  27.         PageCount = dsP.PageCount;  
  28.   
  29.         //create the linkbuttons for pagination  
  30.         BuildPagination();  
  31.     }  
  32. }  


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.

view plaincopy to clipboardprint?
  1. //create the linkbuttons for pagination  
  2. protected void BuildPagination()  
  3. {  
  4.     pnlPager.Controls.Clear(); //  
  5.   
  6.     if (PageCount <= 1) return// at least two pages should be there to show the pagination  
  7.   
  8.     //finding the first linkbutton to be shown in the current display  
  9.     int start = CurrentPageIndex - (CurrentPageIndex % ButtonsCount);  
  10.   
  11.     //finding the last linkbutton to be shown in the current display  
  12.     int end = CurrentPageIndex + (ButtonsCount - (CurrentPageIndex % ButtonsCount));  
  13.   
  14.     //if the start button is more than the number of buttons. If the start button is 11 we have to show the <<First link  
  15.     if (start > ButtonsCount - 1)  
  16.     {  
  17.         pnlPager.Controls.Add(createButton(FirstPageText, 0));  
  18.         pnlPager.Controls.Add(createButton("..", start - 1));  
  19.     }  
  20.   
  21.     int i = 0, j = 0;  
  22.   
  23.     for (i = start; i < end; i++)  
  24.     {  
  25.         LinkButton lnk;  
  26.         if (i < PageCount)  
  27.         {  
  28.             if (i == CurrentPageIndex) //if its the current page  
  29.             {  
  30.                 Label lbl = new Label();  
  31.                 lbl.Text = (i + 1).ToString();  
  32.                 pnlPager.Controls.Add(lbl);  
  33.             }  
  34.             else  
  35.             {  
  36.                 pnlPager.Controls.Add(createButton((i + 1).ToString(), i));  
  37.             }  
  38.         }  
  39.         j++;  
  40.     }  
  41.   
  42.     //If the total number of pages are greaer than the end page we have to show Last>> link  
  43.     if (PageCount > end)  
  44.     {  
  45.         pnlPager.Controls.Add(createButton("..", i));  
  46.         pnlPager.Controls.Add(createButton(">>", PageCount - 1));  
  47.     }  
  48. }  



Thisis the whole logic for a simple implementation. I am using this custompagination in a number of projects and they are working great.

原创粉丝点击