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

Extend Repeater to support DataPager

Rate me:
Please Sign up or sign in to vote.
4.56/5 (16 votes)
21 Oct 2011CPOL 122.2K   2.7K   20   45
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:

C#
[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:

ASP.NET
<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:

C#
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)


Written By
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

 
QuestionBug Fix? Pin
Steve M Anderson1-Oct-13 6:41
Steve M Anderson1-Oct-13 6:41 
SuggestionGuide when not using presistentDataSource or when using datasource paging Pin
Mr Orange25-Jun-13 4:06
Mr Orange25-Jun-13 4:06 
Questionits working very well but taking too much time server Pin
navnit.8825-Jun-13 0:12
navnit.8825-Jun-13 0:12 
AnswerRe: its working very well but taking too much time server Pin
Mr Orange25-Jun-13 1:30
Mr Orange25-Jun-13 1:30 
GeneralRe: its working very well but taking too much time server Pin
navnit.8825-Jun-13 1:51
navnit.8825-Jun-13 1:51 
GeneralRe: its working very well but taking too much time server Pin
Mr Orange25-Jun-13 2:47
Mr Orange25-Jun-13 2:47 
GeneralRe: its working very well but taking too much time server Pin
navnit.8825-Jun-13 2:59
navnit.8825-Jun-13 2:59 
GeneralRe: its working very well but taking too much time server Pin
Mr Orange25-Jun-13 3:49
Mr Orange25-Jun-13 3:49 
The following code will guide you to how to work with databind when not using presistentDataSource
In default.aspx.cs (the codebehind file) of the download example.
Replace entire page code with this and you see how it works.


C#
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class _Default : System.Web.UI.Page 
{
    //Store current page item index (to minimize extra dataloads)
    private int currentIndex;

    protected void Page_Load(object sender, EventArgs e)
    {
        //**2 events required when PersistentDataSource = false or when using datasource paging
        //Runs on all postbacks
        rep1.FetchingData += new EventHandler<PageEventArgs>(rep1_FetchingData);
        //Runs on every page load/posback
        rep1.PreRender += new EventHandler(rep1_PreRender);

        
        if
        (!IsPostBack)
        {
            rep1.PersistentDataSource = false;

            //this is the fake database or other data source
            System.Collections.SortedList SL = new System.Collections.SortedList();


            ////***DATASOURCE PAGING***
            //rep1.PagingInDataSource = true;
            ////filter initial load to contain only first page data eg. Select top 2 * from SOMETABLE
            //SL.Add("val0", "Text0");
            //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");
            ////SL.Add("val10", "Text10");
            
            ////YOU NEED TO OBTAIN TOTAL ROWS WITHOUT PAGING SINCE DATASOURCE ONLY CONTAINS FILTERED (in this 2) ROWS
            //rep1.TotalRows = 11;


            //**PAGING IN REPEATER ALWAYS LOAD DATA PersistentDataSource = false; 
            rep1.PagingInDataSource = false;
            SL.Add("val0", "Text0");
            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");
            SL.Add("val10", "Text10");

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

        }
    }


    protected void rep1_TotalRowCountAvailable(object sender, PageEventArgs e)
    {

    }


    protected void rep1_FetchingData(object sender, PageEventArgs e)
    {
        //Store current page item index (to minimize extra dataloads)
        currentIndex = rep1.StartRowIndex;

        
        //Rebind data on postback so controls in pager are availabel
        //Here you need to get the current page data before you clicked any buttons 
        //So the page will render ok again and you can acess controls in the repeater


        ////***DATASOURCE PAGING***
        ////HERE YOU NEED TO SET YOUR OWN DATASOURCE PAGING LOGIC SO YOU ONLY!! RETURN CURRENT PAGE ITEMS!!!!!
        ////rep1.StartRowIndex = startrow to get from datasource eg 15. MaximumRows = rows on each page eg 5 (then you get row 15 to 20 from datasource) 
        ////this is the fake database or other data source
        //System.Collections.SortedList SL = new System.Collections.SortedList();
        //for (int i = 0; i < rep1.MaximumRows; i++)
        //{
        //    SL.Add("val" + (rep1.StartRowIndex + i).ToString(), "Text" + (rep1.StartRowIndex + i).ToString());
        //}

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

        //**PAGING IN REPEATER ALWAYS LOAD DATA PersistentDataSource = false; 
        System.Collections.SortedList SL = new System.Collections.SortedList();
        SL.Add("val0", "Text0");
        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");
        SL.Add("val10", "Text10");
        rep1.DataSource = SL;
        rep1.DataBind();

    }

    void rep1_PreRender(object sender, EventArgs e)
    {
        //Here you get the new rowindex after paging
        //If currentIndex == rep1.StartRowIndex you already have loaded the data (on first pageload or in rep1_FetchingData on postback)
        //if currentIndex != rep1.StartRowIndex then the pageing have changed

        if (currentIndex != rep1.StartRowIndex)
        {
            ////***DATASOURCE PAGING***
            //System.Collections.SortedList SL = new System.Collections.SortedList();
            //for (int i = 0; i < rep1.MaximumRows; i++)
            //{

            //    SL.Add("val" + (rep1.StartRowIndex + i).ToString(), "Text" + (rep1.StartRowIndex + i).ToString());

            //}

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

            //**PAGING IN REPEATER ALWAYS LOAD DATA PersistentDataSource = false; 
            System.Collections.SortedList SL = new System.Collections.SortedList();
            SL.Add("val0", "Text0");
            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");
            SL.Add("val10", "Text10");
            rep1.DataSource = SL;
            rep1.DataBind();
        }
    }


    protected void Button1_Click(object sender, EventArgs e)
    {

    }
    protected void Button2_Click(object sender, EventArgs e)
    {
   
    }
}

GeneralRe: its working very well but taking too much time server Pin
navnit.8825-Jun-13 18:00
navnit.8825-Jun-13 18:00 
GeneralRe: its working very well but taking too much time server Pin
Mr Orange25-Jun-13 3:30
Mr Orange25-Jun-13 3:30 
QuestionHierarchy repeater Pin
Tyng17-Jan-13 12:12
Tyng17-Jan-13 12:12 
BugI think, logic is not valid. Pin
Mikhail Polyakov7-Sep-12 0:37
Mikhail Polyakov7-Sep-12 0:37 
GeneralRe: I think, logic is not valid. Pin
Mr Orange25-Jun-13 1:37
Mr Orange25-Jun-13 1:37 
Questioni can not use it Pin
sara zo17-Aug-12 23:21
professionalsara zo17-Aug-12 23:21 
AnswerRe: i can not use it Pin
Mr Orange21-Aug-12 4:06
Mr Orange21-Aug-12 4:06 
GeneralMy vote of 4 Pin
kiran dangar21-Oct-11 19:03
kiran dangar21-Oct-11 19:03 
QuestionSqlDataReader Pin
SkillZ9119-Oct-11 5:51
SkillZ9119-Oct-11 5:51 
AnswerRe: SqlDataReader Pin
Mr Orange23-Oct-11 20:57
Mr Orange23-Oct-11 20:57 
Generalone step behind Pin
Member 778573324-May-11 0:19
Member 778573324-May-11 0:19 
GeneralRe: one step behind Pin
Mr Orange26-May-11 20:54
Mr Orange26-May-11 20:54 
GeneralFantastic work Pin
kcabrams22-Sep-10 8:44
kcabrams22-Sep-10 8:44 
QuestionPagingInDataSource Issue Pin
ChadBoettcher29-Jun-10 14:15
ChadBoettcher29-Jun-10 14:15 
AnswerRe: PagingInDataSource Issue Pin
ChadBoettcher26-Jul-10 17:52
ChadBoettcher26-Jul-10 17:52 
GeneralDataPagerRepeater inside UpdatePanel Pin
ran97-Jun-10 10:49
ran97-Jun-10 10:49 
GeneralRe: DataPagerRepeater inside UpdatePanel Pin
Mr Orange7-Jun-10 21:47
Mr Orange7-Jun-10 21:47 

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.