Click here to Skip to main content
15,885,216 members
Articles / Web Development / ASP.NET

Allow Paging in Repeater and DataList Using C#

Rate me:
Please Sign up or sign in to vote.
4.89/5 (17 votes)
27 Aug 2009CPOL2 min read 105.3K   4.5K   41   25
A Web User Control to enable paging in Repeater and DataList controls.

Introduction

The Repeater and DataList are two very important controls and many developers use at least one of them in most of their websites or web applications. Also, these Controls are easy to use for UI developers, but their features are limited. The Gridview control has a very useful paging feature, but it's a complex control and you might not want to use it. And, if you want to use a Repeater or a DataList, they do not support paging, so what will you do ?!!

I will show you how to make a Repeater or a DataList that allows paging. I'll also make a web user control that anyone can use to allow paging in a Repeater or DataList control.

How to Decide to Use the DataGrid, DataList, or Repeater

The URL below describes the differences between a DataGrid, DataList, and a Repeater and helps to decide which one to use: http://msdn.microsoft.com/en-us/library/aa479015.aspx.

Advantages of the AllowPaging Control

It enables paging in Repeater and DataList controls. It is an easy to use Web User Control. The control depends on ObjectDataSource so, if you want to use SqlDataSource, just replace ObjectDataSource with SqlDataSource.

Using the code

The sample code is divided into three parts:

  1. The Default Page to add a Repeater or DataList control.
  2. The UCPager Web User Control that is responsible for paging.
  3. The GetData class to get data from the database.

Also, I'm using the Northwind database in this sample.

First, I'll talk about the User Control sample code. There are four important properties:

  • CurrentPage - sets or gets the current page number
  • Ods - pass to it the ObjectDataSource
  • ObjectControl - pass a Repeater or DataList
  • PageSize - pass an integer for how many records will appear in each page
C#
/// <summary>
/// set or get the Current Page Number
/// </summary>
public int CurrentPage
{
    get
    {
        //get current page number
        object obj = this.ViewState["_CurrentPage"];
        if (obj == null)
        {
            return 0;
        }
        else
        {
            return (int)obj;
        }
    }
    set
    {
        //set in viewstate the current page number
        this.ViewState["_CurrentPage"] = value;
    }
}

/// <summary>
/// set or get ObjectDataSource that's use to bind the control
/// Like(Repeater or Datalist)
/// </summary>
public ObjectDataSource Ods { get; set; }

/// <summary>
/// set or get Control Name EX. (Repeater1 or Datalist1)
/// </summary>
public object ObjectControl { get; set; }

/// <summary>
/// set or get count of pages
/// page size determine how many records will appears in every page
/// </summary>
public int PageSize { get; set; }

The method below is responsible for binding a control and enabling or disabling the navigation buttons depending on how records are shown.

C#
/// <summary>
/// bind controls with data
/// enable and disable controls depending on page number
/// check for object Control if it a Repeater or a DataList
/// </summary>
/// <returns>the count of pages</returns>
private int GetItems()
{
    //create new instance of PagedDataSource
    PagedDataSource objPds = new PagedDataSource();
    //set number of pages will appear
    objPds.PageSize = PageSize;
    objPds.DataSource = Ods.Select();
    objPds.AllowPaging = true;
    int count = objPds.PageCount;
    objPds.CurrentPageIndex = CurrentPage;
    if (objPds.Count > 0)
    {
        //dispaly controls if there are pages
        btnPrevious.Visible = true;
        btnNext.Visible = true;
        btnLastRecord.Visible = true;
        btnFirstRecord.Visible = true;
        lblCurrentPage.Visible = true;
        lblCurrentPage.Text = "Page " + 
          Convert.ToString(CurrentPage + 1) + " of " + 
          Convert.ToString(objPds.PageCount);
    }
    else
    {
        //disable controls if there are no pages
        btnPrevious.Visible = false;
        btnNext.Visible = false;
        btnLastRecord.Visible = false;
        btnFirstRecord.Visible = false;
        lblCurrentPage.Visible = false;
    }
    btnPrevious.Enabled = !objPds.IsFirstPage;
    btnNext.Enabled = !objPds.IsLastPage;
    btnLastRecord.Enabled = !objPds.IsLastPage;
    btnFirstRecord.Enabled = !objPds.IsFirstPage;
    //check for object control if it a DataList
    //we will use DList Variable
    if (ObjectControl is DataList)
    {
        DList = (DataList)ObjectControl;
        DList.DataSource = objPds;
        DList.DataBind();
    }
    //check for object control if it a Repeater
    //we will use Rep Variable
    else if (ObjectControl is Repeater)
    {
        Rep = (Repeater)ObjectControl;
        Rep.DataSource = objPds;
        Rep.DataBind();
    }
    return count;
}

In the First Record button:

C#
//go to first Page
CurrentPage = 0;

GetItems();

In the Last Record button:

C#
//go to last page
CurrentPage = GetItems() -1;

GetItems();

In the Previous Record button:

C#
//back to previous page
CurrentPage -= 1;

GetItems();

In the Next Record button:

C#
//go to next page
CurrentPage += 1;

GetItems();

In the Default Page, all you need is bind the AllowPaging control properties, and the code below explains that:

C#
UCPager1.Ods = ObjEmployees;

UCPager1.ObjectControl = Repeater1;

UCPager1.PageSize = 1;

I'm waiting for your comments. You can read my blog here: http://waleedelkot.blogspot.com/.

Points of Interest

This control saves a lot of time for me, and I hope it helps you too.

History

  • 27-08-2009: First version released.

License

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


Written By
Software Developer (Senior) Equinox Web
Egypt Egypt
I have 5 years experience working as a Software Developer. I have a wide range of experience in programming and I am skilled in the use of Visual Studio.NET 2008, Windows AppLication, Web Application, Web Services, Windows Services, WPF, HTML, Java Script, Ajax, ASP.NET, DevExpress Controls, Office Application Programmability in Visual Studio.NET 2008, creating web and windows applications using C#.NET and experienced in using all Microsoft Office Applications.

Comments and Discussions

 
AnswerPaging with Repeater control in ASP.NET Pin
ChandanBadgujar17-Jun-13 5:09
ChandanBadgujar17-Jun-13 5:09 
QuestionThanks!!! Pin
pradippr11-May-13 3:12
pradippr11-May-13 3:12 
QuestionAbout DataSource Pin
csharpbd3-Feb-13 8:22
professionalcsharpbd3-Feb-13 8:22 
GeneralMy vote of 5 Pin
csharpbd3-Feb-13 8:20
professionalcsharpbd3-Feb-13 8:20 
QuestionHow to put paging in the top and the bottom of the repeater Pin
Tavob4-Jan-13 4:38
Tavob4-Jan-13 4:38 
GeneralMy vote of 4 Pin
nipunasilva23-May-11 23:59
nipunasilva23-May-11 23:59 
GeneralThank you very much Pin
viet200613-May-11 5:01
viet200613-May-11 5:01 
Generalit not working in gridview Pin
sameer das23-Oct-10 1:28
sameer das23-Oct-10 1:28 
GeneralAsp.Net Pin
Prerna Gakhar23-Sep-10 3:49
Prerna Gakhar23-Sep-10 3:49 
QuestionAsp.net(C#) Pin
Prerna Gakhar23-Sep-10 3:45
Prerna Gakhar23-Sep-10 3:45 
GeneralAccess Database Pin
UKMelody23-Mar-10 11:20
UKMelody23-Mar-10 11:20 
GeneralHelp Please! Pin
UKMelody22-Mar-10 14:05
UKMelody22-Mar-10 14:05 
GeneralQuite Usefull Pin
ozgurkaplan22-Feb-10 3:24
ozgurkaplan22-Feb-10 3:24 
GeneralMy problem is this I need to display 3 records across and 5 rows down Pin
mikelopilato30-Sep-09 11:49
mikelopilato30-Sep-09 11:49 
GeneralThis is also helpful for paging with datalist Pin
Rajendra Malav2-Sep-09 1:34
Rajendra Malav2-Sep-09 1:34 
GeneralRe: This is also helpful for paging with datalist Pin
Waleed Elkot2-Sep-09 2:51
Waleed Elkot2-Sep-09 2:51 
GeneralRe: This is also helpful for paging with datalist Pin
nskwaits30-Sep-09 22:14
nskwaits30-Sep-09 22:14 
GeneralCompile Code Pin
hulkonline30-Aug-09 23:31
hulkonline30-Aug-09 23:31 
can I get dll output from web user control?

thanks
GeneralRe: Compile Code Pin
Waleed Elkot30-Aug-09 23:41
Waleed Elkot30-Aug-09 23:41 
Generalcool Pin
Ahmed Elkot27-Aug-09 15:55
Ahmed Elkot27-Aug-09 15:55 
GeneralRe: cool Pin
Waleed Elkot27-Aug-09 15:57
Waleed Elkot27-Aug-09 15:57 
GeneralNice Pin
Wael Hussein27-Aug-09 15:46
Wael Hussein27-Aug-09 15:46 
GeneralRe: Nice Pin
Waleed Elkot27-Aug-09 15:58
Waleed Elkot27-Aug-09 15:58 
GeneralRe: Nice Pin
Amr Saafan28-Aug-09 2:06
Amr Saafan28-Aug-09 2:06 
GeneralRe: Nice Pin
Waleed Elkot28-Aug-09 14:45
Waleed Elkot28-Aug-09 14:45 

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.