Click here to Skip to main content
12,700,558 members (31,617 online)
Click here to Skip to main content
Add your own
alternative version

Tagged as


21 bookmarked

Extend Repeater to support DataPager

, 21 Oct 2011 CPOL
Rate this:
Please Sign up or sign in to vote.
How to extend Repeater to support DataPager.


I have seen some examples of making a Repeater to work with a DataPager like it does with the ListView control. But all examples failed when I tried them, and I spent some time to get this to work. Here is my solution.

Using the code

First, create a Custom Web Control with the name DataPagerRepeater. Inherit it from Repeater and include the System.Web.UI.WebControls.IPageableItemContainer namespace (this requires that you add a reference to System.Web.Extensions).

Add this code:

[ToolboxData("<{0}:DataPagerRepeater runat="server" 

public class DataPagerRepeater : Repeater, 
       System.Web.UI.WebControls.IPageableItemContainer, INamingContainer

    public int MaximumRows { get { return ViewState["_maximumRows"] != null ? 
                                 (int)ViewState["_maximumRows"] : -1; } }
    public int StartRowIndex { get { return ViewState["_startRowIndex"] != null ? 
                              (int)ViewState["_startRowIndex"] : -1; } }
    public int TotalRows { get { return ViewState["_totalRows"] != null ? 
                               (int)ViewState["_totalRows"] : -1; } }

    public bool PersistentDataSource { 
        get { return ViewState["PersistentDataSource"] != null ? 
              (bool)ViewState["PersistentDataSource"] : true; }
        set { ViewState["PersistentDataSource"] = value; }

    protected override void LoadViewState(object savedState)

        if (Page.IsPostBack)
            if (PersistentDataSource && ViewState["DataSource"] != null)
                this.DataSource = ViewState["DataSource"];

    public void SetPageProperties(int startRowIndex, int maximumRows, bool databind)
        ViewState["_startRowIndex"] = startRowIndex;
        ViewState["_maximumRows"] = maximumRows;

        if (TotalRows > -1)
            if (TotalRowCountAvailable != null)
                   new PageEventArgs((int)ViewState["_startRowIndex"], 
                   (int)ViewState["_maximumRows"], TotalRows));

    protected override void OnDataPropertyChanged()
        if (MaximumRows != -1)
            this.RequiresDataBinding = true;

    protected override void RenderChildren(HtmlTextWriter writer)
        if (MaximumRows != -1)
            foreach (RepeaterItem item in this.Items)
                if (item.ItemType == ListItemType.Item || 
                    item.ItemType == ListItemType.AlternatingItem)
                    item.Visible = false;

                    if (item.ItemIndex >= (int)ViewState["_startRowIndex"] && 
                        item.ItemIndex <= ((int)ViewState["_startRowIndex"] + 
                        item.Visible = true;
                    item.Visible = true;

    public override void DataBind()

        if (MaximumRows != -1)
            int i = 0;
            foreach (object o in GetData())
            ViewState["_totalRows"] = i;

                ViewState["DataSource"] = this.DataSource;
            SetPageProperties(StartRowIndex, MaximumRows, true);

    protected override System.Collections.IEnumerable GetData()
        return base.GetData();

    public event System.EventHandler<PageEventArgs> TotalRowCountAvailable;

Build and use like this in an ASP.NET Form:

<cc1:DataPagerRepeater ID="rep1" 
    runat="server" PersistentDataSource="true"> 
<HeaderTemplate > <div></HeaderTemplate> 
 <div >
 <%# Eval("Value") %>

<asp:DataPager ID="DataPager1" 
  PagedControlID="rep1" PageSize="2" runat="server">
<asp:NextPreviousPagerField ButtonType="Button" ShowFirstPageButton="True" 
   ShowNextPageButton="False" ShowPreviousPageButton="False" />

<asp:NumericPagerField />
<asp:NextPreviousPagerField ButtonType="Button" ShowLastPageButton="True" 
        ShowNextPageButton="False" ShowPreviousPageButton="False" />


And in the code-behind:

protected void Page_Load(object sender, EventArgs e) 
        System.Collections.SortedList SL = new System.Collections.SortedList();


        rep1.DataSource = SL;

That's all!


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


About the Author

Mr Orange
Software Developer (Senior) Sigma IT & Management
Sweden Sweden
Working with web application development since 1999.
Developer of the CMS product Publech ( and a large range of other Publech modules.

Involved in developing the largest non commercial website in Sweden the Swedish Public Employment Service (

You may also be interested in...


Comments and Discussions

QuestionBug Fix? Pin
Steve M Anderson1-Oct-13 7:41
memberSteve M Anderson1-Oct-13 7:41 
SuggestionGuide when not using presistentDataSource or when using datasource paging Pin
Mr Orange25-Jun-13 5:06
memberMr Orange25-Jun-13 5:06 
Questionits working very well but taking too much time server Pin
navnit.8825-Jun-13 1:12
membernavnit.8825-Jun-13 1:12 
AnswerRe: its working very well but taking too much time server Pin
Mr Orange25-Jun-13 2:30
memberMr Orange25-Jun-13 2:30 
GeneralRe: its working very well but taking too much time server Pin
navnit.8825-Jun-13 2:51
membernavnit.8825-Jun-13 2:51 
GeneralRe: its working very well but taking too much time server Pin
Mr Orange25-Jun-13 3:47
memberMr Orange25-Jun-13 3:47 
GeneralRe: its working very well but taking too much time server Pin
navnit.8825-Jun-13 3:59
membernavnit.8825-Jun-13 3:59 
GeneralRe: its working very well but taking too much time server Pin
Mr Orange25-Jun-13 4:49
memberMr Orange25-Jun-13 4:49 
GeneralRe: its working very well but taking too much time server Pin
navnit.8825-Jun-13 19:00
membernavnit.8825-Jun-13 19:00 
GeneralRe: its working very well but taking too much time server Pin
Mr Orange25-Jun-13 4:30
memberMr Orange25-Jun-13 4:30 
QuestionHierarchy repeater Pin
Tyng17-Jan-13 13:12
memberTyng17-Jan-13 13:12 
BugI think, logic is not valid. Pin
Mikhail Polyakov7-Sep-12 1:37
memberMikhail Polyakov7-Sep-12 1:37 
GeneralRe: I think, logic is not valid. Pin
Mr Orange25-Jun-13 2:37
memberMr Orange25-Jun-13 2:37 
Questioni can not use it Pin
sara zo18-Aug-12 0:21
membersara zo18-Aug-12 0:21 
AnswerRe: i can not use it Pin
Mr Orange21-Aug-12 5:06
memberMr Orange21-Aug-12 5:06 
GeneralMy vote of 4 Pin
kiran dangar21-Oct-11 20:03
memberkiran dangar21-Oct-11 20:03 
QuestionSqlDataReader Pin
SkillZ9119-Oct-11 6:51
memberSkillZ9119-Oct-11 6:51 
AnswerRe: SqlDataReader Pin
Mr Orange23-Oct-11 21:57
memberMr Orange23-Oct-11 21:57 
Generalone step behind Pin
Member 778573324-May-11 1:19
memberMember 778573324-May-11 1:19 
GeneralRe: one step behind Pin
Mr Orange26-May-11 21:54
memberMr Orange26-May-11 21:54 
GeneralFantastic work Pin
kcabrams22-Sep-10 9:44
memberkcabrams22-Sep-10 9:44 
QuestionPagingInDataSource Issue Pin
ChadBoettcher29-Jun-10 15:15
memberChadBoettcher29-Jun-10 15:15 
AnswerRe: PagingInDataSource Issue Pin
ChadBoettcher26-Jul-10 18:52
memberChadBoettcher26-Jul-10 18:52 
GeneralDataPagerRepeater inside UpdatePanel Pin
ran97-Jun-10 11:49
memberran97-Jun-10 11:49 
GeneralRe: DataPagerRepeater inside UpdatePanel Pin
Mr Orange7-Jun-10 22:47
memberMr Orange7-Jun-10 22:47 
GeneralRe: DataPagerRepeater inside UpdatePanel Pin
ran98-Jun-10 10:40
memberran98-Jun-10 10:40 
GeneralRe: DataPagerRepeater inside UpdatePanel Pin
Mr Orange8-Jun-10 22:30
memberMr Orange8-Jun-10 22:30 
GeneralRe: DataPagerRepeater inside UpdatePanel Pin
ran914-Jun-10 8:26
memberran914-Jun-10 8:26 
GeneralRe: DataPagerRepeater inside UpdatePanel Pin
ran914-Jun-10 8:29
memberran914-Jun-10 8:29 
GeneralRe: DataPagerRepeater inside UpdatePanel Pin
ran95-Jul-10 11:31
memberran95-Jul-10 11:31 
GeneralMy vote of 1 Pin
Sebastien Lorion27-May-10 6:56
memberSebastien Lorion27-May-10 6:56 
GeneralSome changes new version Pin
Mr Orange7-Apr-10 7:11
memberMr Orange7-Apr-10 7:11 
GeneralRe: Some changes new version Pin
Mr Orange7-Apr-10 7:38
memberMr Orange7-Apr-10 7:38 
QuestionBug? [modified] Pin
ran920-Mar-10 12:56
memberran920-Mar-10 12:56 
AnswerRe: Bug? [modified] Pin
xatazch6-Apr-10 2:22
memberxatazch6-Apr-10 2:22 
I have the same problem. anyone know how to solve this?
I get this problem when i try to Connect the DataPager to my DataPagerRepeater.

How do i solve this?

if i don´t have this in my code the DataPagerRepeater work.

<asp:DataPager ID="DataPager" PagedControlID="rep1" runat="server" PageSize="2">
<asp:NumericPagerField />

modified on Tuesday, April 6, 2010 8:10 AM

GeneralRe: Bug? Pin
ran96-Apr-10 9:29
memberran96-Apr-10 9:29 
GeneralRe: Bug? Pin
Mr Orange7-Apr-10 7:12
memberMr Orange7-Apr-10 7:12 
GeneralRe: Bug? Pin
xatazch9-Apr-10 1:32
memberxatazch9-Apr-10 1:32 
GeneralRe: Bug? Pin
Mr Orange21-Apr-10 6:50
memberMr Orange21-Apr-10 6:50 
GeneralRe: Bug? Pin
ran96-Jun-10 10:47
memberran96-Jun-10 10:47 
GeneralOne bug Pin
badalpatel9518-Mar-10 18:58
memberbadalpatel9518-Mar-10 18:58 
GeneralRe: One bug Pin
Mr Orange7-Apr-10 7:15
memberMr Orange7-Apr-10 7:15 
GeneralThanks Pin
Niladri_Biswas8-Feb-10 1:10
memberNiladri_Biswas8-Feb-10 1:10 
Generalin VB. Pin
B-One19-Jan-10 23:17
memberB-One19-Jan-10 23:17 
GeneralRe: in VB. Pin
B-One20-Jan-10 1:41
memberB-One20-Jan-10 1:41 

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.

| Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.170118.1 | Last Updated 21 Oct 2011
Article Copyright 2009 by Mr Orange
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid