Click here to Skip to main content
15,349,128 members
Articles / Web Development / ASP.NET
Posted 14 Jul 2009


55 bookmarked

Repeater with Paging and Sorting Features

Rate me:
Please Sign up or sign in to vote.
4.78/5 (17 votes)
16 Jul 2009CPOL3 min read
Repeater with Paging and Sorting features


In order to create a dynamic page, we need some server side control to do repetitive work and create the page. There are lots of controls provided by Microsoft .NET, i.e. DataList, DataGrid, GridView, Repeater and many more.


I love to use Repeater as it is light weight and if I need it only for showing data. One of the drawbacks of using Repeater is that it does not have in-built functionality for Paging and Sorting. This article will be helpful to people who need a light weight control and also want to use Paging and Sorting.

This article will show you how to implement Paging and Sorting in the Repeater. I also have a working demo for you. You can download it form here.

Using the Code

We are going to use the following features and class:

  • PagedDataSource: This class encapsulates all the properties for paging of a data-bound control which allows it to perform paging. This class is sealed so there is no inheritance.
  • Repeater: A light weight data-bound control which allows us to create dynamic data using custom layout without Paging support.
  • Extension Methods: One of the cool functionalities of C# 3.0, allows us to create our own methods on already existing class without inheritance. You can find more on Extension methods here. We will use few extension methods for sorting.

First we should create a data source to which we are going to assign PagedDataSource object. Let's create one public property named UserList by generating some random values.

IList<UserInfo> list;

for (int i = 1; i <= 100; i++)
    list.Add(new UserInfo()
        FirstName = string.Format("First Name : {0}", r.Next(6 * i, 60 * i + 3)),
        LastName = string.Format("Last Name : {0}", r.Next(11 * i, 60 * i + 7)),
        Password = string.Format("Password : {0}", r.Next(13 * i, 60 * i + 11)),
        UserName = string.Format("UserName : {0}", r.Next(17 * i, 60 * i + 23)),
        LastLoginDate = DateTime.Now.AddMinutes(r.Next(7 * i, 12 * i + 13)),
return list;

We have a data source ready with us, the next step will write FillRepeater method which creates an object of PagedDataSource set DataSource property. Along with that, we have to set AllowPaging properties, which indicate whether paging is enabled in data-bound or not.

//Create the object of PagedDataSource
PagedDataSource objPds = new PagedDataSource();

//Assign our data source to PagedDataSource object
objPds.DataSource = UserList;

//Set the allow paging to true
objPds.AllowPaging = true;

//Set the number of items you want to show
objPds.PageSize = 7;

//Disable Prev, Next buttons if necessary
lbtnPrev.Enabled = !objPds.IsFirstPage;
lbtnNext.Enabled = !objPds.IsLastPage;

//Assign PagedDataSource to repeater
rptUsers.DataSource = objPds;

While doing paging, we always need to take care about lower bound and upper bound; to have navigation, PagedDataSource provides two getter properties IsFirstPage and IsLastPage, which allow you to track your navigation. Let's create handlers for Next and Previous buttons.

protected void lbtnPrev_Click(object sender, EventArgs e)
    //Decrement page by one
    //Fill repeater

protected void lbtnNext_Click(object sender, EventArgs e)
    //Increment page by one
    //Fill repeater

NowViewing is the property which maintains the current viewing page into viewstate:

public int NowViewing
        object obj = ViewState["_NowViewing"];
        if (obj == null)
            return 0;
            return (int)obj;
        this.ViewState["_NowViewing"] = value;

We are done with simple Paging with repeater. Here is the output for simple paging:


You can also add First and Last link, IsFirstPage and IsLastPage can help you to manage the visibility for links.

lbtnFirst.Enabled = !objPds.IsFirstPage;
lbtnLast.Enabled = !objPds.IsLastPage;

Also you can set the viewing message by using NowViewing property and PageCount of PagedDataSource.

//Change the text Now viewing text
lblCurrentPage.Text = "Now viewing : " + (NowViewing + 1).ToString() +
				" of " + objPds.PageCount.ToString();

For pager, you just need to set PageSize property.

//Set the number of items you want to show
objPds.PageSize = int.Parse(ddlPageSize.SelectedValue);

Following is the image which will show how the page will look like after adding pager, now viewing message, first and last link.


So far so good, now let's add a sorting feature to our page. We are going to use Extension Methods a cool feature of C# 3.0. I created the following 5 extension methods to implement the sorting.

public static IList<UserInfo> OrderByLastLoginDate
		(this IList<UserInfo> userList, bool isAscending)
public static IList<UserInfo> OrderByPassword
		(this IList<UserInfo> userList, bool isAscending)
public static IList<UserInfo> OrderByLastName
		(this IList<UserInfo> userList, bool isAscending)
public static IList<UserInfo> OrderByFirstName
		(this IList<UserInfo> userList, bool isAscending)
public static IList<UserInfo> OrderByUserName
		(this IList<UserInfo> userList, bool isAscending)

The first argument is list and another is IList<UserInfo> and the other is isAscending, which decides the order. Each of these methods are using Lambda expression and two extension methods OrderBy and OrderByDescending. Let's look into one of the methods.

public static IList<UserInfo> OrderByUserName
	(this IList<UserInfo> userList, bool isAscending)
    if (isAscending)
        return userList.OrderBy(a => a.UserName).ToList();
        return userList.OrderByDescending(a => a.UserName).ToList();

We created extension methods for sorting, let's use those methods to get sorting done.

We are going to use two viewstate objects ViewState["SortExpression"] which stores the sort expression and ViewState["SortDetails"] which is Hashtable to store sort order for field. After applying some logic, we can finally have sortExpression to identify on which field we need sorting and isAscending which is used to decide order. Based on this information, the following code will call extension method which does the sorting and binds the data to PagedDataSource.

switch (sortExpression)
    case "UserName":
        return list.OrderByUserName(isAscending);
    case "FirstName":
        return list.OrderByFirstName(isAscending);
    case "LastName":
        return list.OrderByLastName(isAscending);
    case "Password":
        return list.OrderByPassword(isAscending);
    case "LastLoginDate":
        return list.OrderByLastLoginDate(isAscending);
        return list.OrderByUserName(isAscending);

Simple right? Ok, so we are done with sorting too. 

Points of Interest

Use of Extension method helps to write smart code, also the PagedDataSource is flexible for sorting and paging.


  • 14th July, 2009: Initial post
  • 15th July, 2009: Article updated


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

Team Leader Softweb Solutions
India India
Profiles : Code Project, ASP.NET Forums
Blog : Knowledgebase World
Current Company : Softwebsolution INC
User Group : Ahmedabad SQLServer UserGroup
Other : Microsoft Certified Technology Specialist (MCTS)

Comments and Discussions

QuestionThe page seems to be showing duplicate data when we click on Prev, Next etc. Also when you change the page size, it does not go to the first page Pin
ppushpa12-Jun-17 10:49
Memberppushpa12-Jun-17 10:49 
QuestionYou wrote the code for general purpose but it won't work for Data from database Pin
Gopi krishna.ch26-Oct-15 18:07
MemberGopi krishna.ch26-Oct-15 18:07 
SuggestionCan we use reflection to sort the columns Pin
cqcmdwym30-Apr-13 18:32
Membercqcmdwym30-Apr-13 18:32 
QuestionRepeater pagination Pin
anjalisushma29-Mar-13 19:53
Memberanjalisushma29-Mar-13 19:53 
GeneralPager disappears on postback Pin
xsoftdev18-Nov-09 17:45
Memberxsoftdev18-Nov-09 17:45 
Generalkeep sort state Pin
Member 143559713-Sep-09 6:18
MemberMember 143559713-Sep-09 6:18 
GeneralCause of toggling sort order Pin
RS785776614-Jan-10 4:39
MemberRS785776614-Jan-10 4:39 
GeneralTotal record count Pin
ryanoc33315-Jul-09 2:38
Memberryanoc33315-Jul-09 2:38 
GeneralRe: Total record count Pin
bhadeliaimran15-Jul-09 4:32
Memberbhadeliaimran15-Jul-09 4:32 
GeneralRuntime short a selected column Pin
Md. Marufuzzaman14-Jul-09 19:30
professionalMd. Marufuzzaman14-Jul-09 19:30 
GeneralRe: Runtime short a selected column Pin
bhadeliaimran15-Jul-09 4:36
Memberbhadeliaimran15-Jul-09 4:36 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.