How To Use PagedDataSource To Paginate Repeater (Prev/Next/First/Last and Page Numbers)

I needed to add pagination to my Repeater control. The requirements where that a First/Last Next/Prev buttons be available as well as a list of the available pages so users could visit any page they wanted. (1 | 2 | 3..).

Below is the result using a few articles as reference for the implementation:

<%@ Page Language="C#" MasterPageFile="~/admin/paperless/Paperless.master" Inherits="Company.Common.Objects.CompanyPage"
    Title="Paperless Interest" %>

<%@ Import Namespace="System.Collections.Generic" %>
<%@ Import Namespace="System.Xml" %>
<%@ Import Namespace="Admin.Redesign.Layers" %>
<%@ Import Namespace="Admin.Redesign.Objects" %>
<%@ Import Namespace="Company.Common.Layers" %>
<%@ Import Namespace="Company.Common.Objects" %>
<%@ Import Namespace="Company.Common.Objects.DataObjects" %>

<script runat="server">       
    
    protected List<PaperlessActivity> _Reminders = new List<PaperlessActivity>();

    protected int CurrentPage
    {
        get
        {
            // look for current page in ViewState
            object o = this.ViewState["_CurrentPage"];
            if (o == null)
                return 0;	// default to showing the first page
            else
                return (int)o;
        }

        set
        {
            this.ViewState["_CurrentPage"] = value;
        }
    }

    protected int PageCount
    {
        get
        {
            // look for current page count in ViewState
            object o = this.ViewState["_PageCount"];
            if (o == null)
                return 1;	// default to just 1 page
            else
                return (int)o;
        }

        set
        {
            this.ViewState["_PageCount"] = value;
        }
    }


    protected void GetItems()
    {
        PagedDataSource pagedData = new PagedDataSource();

        pagedData.AllowPaging = true;
        pagedData.PageSize = 10;
        pagedData.DataSource = PaperlessLogic.GetInterestList();
        pagedData.CurrentPageIndex = CurrentPage;


        lblCurrentPage.Text = "<b>Page:</b> " + (CurrentPage + 1).ToString() + " of " + pagedData.PageCount.ToString();

        PageCount = pagedData.PageCount;

        // Disable Prev/Next First/Last buttons if necessary
        cmdPrev.Enabled = !pagedData.IsFirstPage;
        cmdFirst.Enabled = !pagedData.IsFirstPage;
        cmdNext.Enabled = !pagedData.IsLastPage;
        cmdLast.Enabled = !pagedData.IsLastPage;



        // Wire up the page numbers
        if (pagedData.PageCount > 1)
        {
            rptPages.Visible = true;
            ArrayList pages = new ArrayList();
            for (int i = 0; i < pagedData.PageCount; i++)
                if (i == CurrentPage)
                {

                    pages.Add("<b>" + (i + 1).ToString() + "</b>");
                }
                else
                {
                    pages.Add((i + 1).ToString());
                }
            rptPages.DataSource = pages;
            rptPages.DataBind();
        }
        else
        {
            rptPages.Visible = false;
        }



        RptPaperless.DataSource = pagedData;
        RptPaperless.DataBind();
    }

    protected void cmdPrev_Click(object sender, System.EventArgs e)
    {
        // Set viewstate variable to the previous page
        CurrentPage -= 1;

        // Reload control
        GetItems();
    }

    protected void cmdNext_Click(object sender, System.EventArgs e)
    {
        // Set viewstate variable to the next page
        CurrentPage += 1;

        // Reload control
        GetItems();
    }

    protected void cmdFirst_Click(object sender, System.EventArgs e)
    {
        // Set viewstate variable to the first page
        CurrentPage = 0;

        // Reload control
        GetItems();
    }
    protected void cmdLast_Click(object sender, System.EventArgs e)
    {
        // Set viewstate variable to the last page
        CurrentPage = PageCount - 1;

        // Reload control
        GetItems();
    }

    protected void rptPages_ItemCommand(object source,
                             RepeaterCommandEventArgs e)
    {
        CurrentPage = Convert.ToInt32(e.CommandArgument) - 1;
        GetItems();
    }

    protected override void OnInit(EventArgs e)
    {
        base.OnInit(e);
        rptPages.ItemCommand +=
           new RepeaterCommandEventHandler(rptPages_ItemCommand);
    }

    protected override void OnLoad(EventArgs e)
    {
        base.OnLoad(e);

        GetItems();
    }

    protected bool CurrentPageHighlight(int currPage)
    {
        return currPage == CurrentPage ? true : false;
    }
</script>

<asp:Content ID="Content1" ContentPlaceHolderID="cphContent" runat="Server">
    <asp:ScriptManager ID="ScriptManager" runat="server" />
    <asp:UpdatePanel ID="UpdatePanel1" UpdateMode="Conditional" runat="server">
        <ContentTemplate>
            <h1>
                Paperless Interest</h1>
            <asp:Repeater ID="RptPaperless" runat="server">
                <HeaderTemplate>
                    <table class="tableData" cellpadding="0" cellspacing="0">
                        <tr>
                            <th>
                                Interest ID
                            </th>
                            <th>
                                Interest Date
                            </th>
                            <th>
                                Utility
                            </th>
                            <th>
                                OpCo
                            </th>
                            <th>
                                State
                            </th>
                            <th>
                                Channel
                            </th>
                            <th>
                                Created By
                            </th>                           
                            <th>
                                Account #
                            </th>
                            <th>
                                Customer Email
                            </th>
                            <th>
                                Customer Name
                            </th>
                        </tr>
                </HeaderTemplate>
                <FooterTemplate>
                    </table>
                </FooterTemplate>
                <ItemTemplate>
                    <tr>
                        <td>
                            <a href="Edit.aspx?id=<%# Eval("InterestID") %>">Edit</a>:
                            <%# Eval("InterestID") %>
                        </td>
                        <td>
                            <%# Eval("CreatedDate", "{0:M/d/yyyy h:mm:ss tt}") %>
                        </td>
                        <td>
                            <%# Eval("UtilityName") %>
                        </td>
                        <td>
                            <%# Eval("OpCoNumber") %>
                        </td>
                        <td>
                            <%# Eval("StateCode") %>
                        </td>
                        <td>
                            <%# Eval("Channel.ChannelName") %>
                        </td>
                        <td>
                            <%# Eval("CreatedBy") %>
                        </td>                        
                        <td>
                            <%# Eval("CustomerAccountNumber") %>
                        </td>
                        <td>
                            <%# Eval("CustomerEmail") %>
                        </td>
                        <td>
                            <%# Eval("CustomerName") %>
                        </td>
                    </tr>
                </ItemTemplate>
            </asp:Repeater>
            <br />
            <center>
                <div>
                    <div>
                        <asp:Label ID="lblCurrentPage" runat="server"></asp:Label></div>
                    <div>
                        <table>
                            <tr>
                                <td>
                                    <asp:Button ID="cmdFirst" runat="server" Text="<< First" OnClick="cmdFirst_Click">
                                    </asp:Button>&nbsp;
                                </td>
                                <td>
                                    <asp:Button ID="cmdPrev" runat="server" Text="< Prev" OnClick="cmdPrev_Click"></asp:Button>&nbsp;
                                </td>
                                <td>
                                    <asp:Repeater ID="rptPages" runat="server">
                                        <HeaderTemplate>
                                            <table cellpadding="0" cellspacing="0" border="0">
                                                <tr class="text">
                                                    <td>
                                        </HeaderTemplate>
                                        <ItemTemplate>
                                            <asp:LinkButton ID="btnPage" CommandName="Page" CommandArgument="<%# Container.DataItem %>"
                                                runat="server"><%# Container.DataItem %>
                                            </asp:LinkButton>&nbsp;
                                        </ItemTemplate>
                                        <FooterTemplate>
                                            </td> </tr> </table>
                                        </FooterTemplate>
                                    </asp:Repeater>
                                </td>
                                <td>
                                    &nbsp;<asp:Button ID="cmdNext" runat="server" Text="Next >" OnClick="cmdNext_Click">
                                    </asp:Button>
                                </td>
                                <td>
                                    &nbsp;<asp:Button ID="cmdLast" runat="server" Text="Last >>" OnClick="cmdLast_Click">
                                    </asp:Button>
                                </td>
                            </tr>
                        </table>
                    </div>
                    &nbsp;</div>
            </center>
        </ContentTemplate>
    </asp:UpdatePanel>
</asp:Content>

References:

Advertisements

2 thoughts on “How To Use PagedDataSource To Paginate Repeater (Prev/Next/First/Last and Page Numbers)

  1. How do you highlight the page numbers or make them bold because
    for (int i = 0; i < pagedData.PageCount; i++)

    if (i == CurrentPage)
    {
    pages.Add("” + (i + 1).ToString() + ““);
    This doesn’t work.

    Where are you using this code :

    protected bool CurrentPageHighlight(int currPage)

    {

    return currPage == CurrentPage ? true : false;

    }
    I tried to use it in repeater like this but no luck

    <asp:LinkButton CssClass='’ ID=”btnPage”

    Is there a solution to make this numbers bold or highlighted ?

  2. Hello, cbehen.

    This code works. I just didn’t use CurrentPageHighlight.

    The magic is here:
    for (int i = 0; i < pagedData.PageCount; i++)
    if (i == CurrentPage)
    {

    pages.Add("” + (i + 1).ToString() + ““);
    }
    else
    {
    pages.Add((i + 1).ToString());
    }

    That’s all. If you have any questions, don’t hesitate to ask

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s