Attention: We are retiring the ASP.NET Community Blogs. Learn more >

ASP.Net Paginator Control

A paginator is often used as a means to navigate through pages. These pages can contain anything you would want. Examples are a) paging through the results of a query or b) paging through a report and c) etc. This article provides the code and a description for a paginator control in ASP.Net and uses the example of paging through results to illustrate how to use this control.

Download the code here (paginator.zip)

I’m assuming everybody already knows what paging exactly is - but if you really don’t know you should look at http://www.google.com/ (thank you for the visitors!)   A quick glance at Google reduces the methods to implement paging into the following two:

  1. retrieve total number of items
  2. retrieve the requested subset of items 

As a bit of a side-track, the following code is how I usually retrieve and bind my data for paging:

   1:  private void BindFooList(int _PageIndex){ 
   2:        int totalrecords = 0; 
   3:        int pagesize = 10; 
   4:        System.Collections.Generic.List<Foo> l = FooService.GetFooList(pagesize, _PageIndex, out totalrecords); 
   5:        rptFooList.DataSource = l; 
   6:        rptFooList.DataBind();
   7:  }

 

As you can see I use a pagesize and a pageindex parameter to specify how many results a page contains and which page I want to retrieve. The totalrecords (out) returns the total number of items found, which, of course, is, usually, different than the number of items actually returned. In the future I will add at least one article about how to limit the number of items returned by the function. The paginator control is specified as:

 

   1:  <cc1:Paginator 
   2:                ID=”pagFooList
   3:                PageSize=”10? 
   4:                PageRadius=”4? 
   5:                TotalRecords=”52? 
   6:                AllowPageSizeChange=”false
   7:                FinishButtonVisible=”true
   8:                FirstButtonTitle=”first
   9:                NextButtonTitle=”next
  10:                PreviousButtonTitle=”previous
  11:                LastButtonTitle=”last
  12:                InfoResultsPerPage=”# of Foo per page
  13:                ResultInfoFormatString=”{0} - {1} of {2}” 
  14:                runat=”server/>

 

Properties

PageSize: How many results are shown on a page
PageRadius: The radius of the number of pages a user can click through
TotalRecords: Total number of items
AllowPageSizeChange: Allow the user to change the number of items displayed
FinishButtonVisible: After the last page allow for a finish button to close the paging
InfoResultsPerPage: The text of the label to change the number of items per page
ResultInfoFormatString: Formatstring of the information label  0) index of first item shown 1) index of last item shown 2) total number of results  

Events

GoToPage: issued when a new page is requested through the paginator.
PageSizeChanged: issued when the page size is changed by the user.
Finish: issued when the user hits the finish button.

When only the control code is written in the .aspx file the paginator will already show up – and after a DataBind() the paginator will display the requested pages. The only thing left to do is to implement the actual paging in the codebehind. We still want to page through our results - right?

 

   1:  private void BindFooList(int _PageIndex)
   2:  { 
   3:        int totalrecords = 0; 
   4:        System.Collections.Generic.List<Foo> l = FooService.GetFooList(pagFooList.PageSize, _PageIndex, out totalrecords); 
   5:        rptFooList.DataSource = l; 
   6:        rptFooList.DataBind(); 
   7:        pagFooList.PageIndex = _PageIndex; 
   8:        pagFooList.TotalRecords = totalrecords; 
   9:        pagFooList.DataBind(); 
  10:  } 
  11:   
  12:  protected override void OnInit(EventArgs e) 
  13:  { 
  14:        pagFooList.GoToPage += new PaginatorEventHandler(pagFooList_GoToPage); 
  15:        pagFooList.PageSizeChanged += new EventHandler(pagFooList_PageSizeChanged); 
  16:        base.OnInit(e); 
  17:  } 
  18:   
  19:  void pagFooList_PageSizeChanged(object sender, EventArgs e) 
  20:  { 
  21:        BindFooList(0); 
  22:  } 
  23:   
  24:  void pagFooList_GoToPage(object sender, PaginatorEventArgs e) 
  25:  { 
  26:        BindFooList(e.NewPage); 
  27:  }

 

Download the whole website (paginator.zip) and please let me know if it was useful to you!

 

Cheers.

CJ. 

 

1 Comment

Comments have been disabled for this content.