Click here to Skip to main content
Click here to Skip to main content
Go to top

Extend Repeater to support DataPager

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

Introduction

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" 
   PersistentDataSource=true></{0}:DataPagerRepeater>")]

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)
    {
        base.LoadViewState(savedState);

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

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

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

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

    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"] + 
                          (int)ViewState["_maximumRows"]))
                    {
                        item.Visible = true;
                    }
                }
                else
                {
                    item.Visible = true;
                }
            }
        }
        base.RenderChildren(writer);
    }

    public override void DataBind()
    { 
        base.DataBind();

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

            if(PersistentDataSource)
                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> 
<ItemTemplate> 
 <div >
 <%# Eval("Value") %>
 </div>
 </ItemTemplate> 
<FooterTemplate></div></FooterTemplate> 
</cc1:DataPagerRepeater> 

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

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

</asp:DataPager>

And in the code-behind:

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

        SL.Add("val1","Text1");
        SL.Add("val2","Text2");
        SL.Add("val3","Text3");
        SL.Add("val4","Text4");
        SL.Add("val5","Text5");
        SL.Add("val6","Text6");
        SL.Add("val7","Text7");
        SL.Add("val8","Text8");
        SL.Add("val9","Text9");

        rep1.DataSource = SL;
        rep1.DataBind();
    } 
}

That's all!

License

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

Share

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 (www.publech.com) and a large range of other Publech modules.
 
Involved in developing the largest non commercial website in Sweden the Swedish Public Employment Service (www.ams.se)

Comments and Discussions

 
GeneralRe: DataPagerRepeater inside UpdatePanel Pinmemberran914-Jun-10 7:26 
GeneralRe: DataPagerRepeater inside UpdatePanel Pinmemberran914-Jun-10 7:29 
GeneralRe: DataPagerRepeater inside UpdatePanel Pinmemberran95-Jul-10 10:31 
GeneralMy vote of 1 PinmemberSebastien Lorion27-May-10 5:56 
GeneralSome changes new version PinmemberMr Orange7-Apr-10 6:11 
GeneralRe: Some changes new version PinmemberMr Orange7-Apr-10 6:38 
QuestionBug? [modified] Pinmemberran920-Mar-10 11:56 
AnswerRe: Bug? [modified] Pinmemberxatazch6-Apr-10 1: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? Pinmemberran96-Apr-10 8:29 
GeneralRe: Bug? PinmemberMr Orange7-Apr-10 6:12 
GeneralRe: Bug? Pinmemberxatazch9-Apr-10 0:32 
GeneralRe: Bug? PinmemberMr Orange21-Apr-10 5:50 
GeneralRe: Bug? Pinmemberran96-Jun-10 9:47 
GeneralOne bug Pinmemberbadalpatel9518-Mar-10 17:58 
GeneralRe: One bug PinmemberMr Orange7-Apr-10 6:15 
GeneralThanks PinmemberNiladri_Biswas8-Feb-10 0:10 
Generalin VB. PinmemberB-One19-Jan-10 22:17 
GeneralRe: in VB. PinmemberB-One20-Jan-10 0:41 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    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 | Mobile
Web03 | 2.8.140926.1 | Last Updated 21 Oct 2011
Article Copyright 2009 by Mr Orange
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid