Search code examples
asp.netpaginationrepeater

Custom pager control


I develop a custom data pager control. Its "previous" and "next" hyperLink buttons don't work properly and I don't understand why.

    <asp:Repeater ID="rpt" runat="server">
<HeaderTemplate>
    <asp:LinkButton ID="lnkbFirst" CommandName="<%#PageChangedItemCommand %>" CommandArgument="1" runat="server">&laquo;</asp:LinkButton>&nbsp;
     <asp:LinkButton ID="lnkbPrevious" CommandName="<%#PageChangedItemCommand %>" CommandArgument="<%#PreviousPageIndex%>"  runat="server">&lt;</asp:LinkButton>&nbsp;
 </HeaderTemplate>   
<ItemTemplate> 
    <asp:LinkButton  CommandName="<%#PageChangedItemCommand %>" CommandArgument="<%#Container.DataItem.ToString()%>"  ID="p" runat="server" ><%#Container.DataItem.ToString()%>&nbsp;</asp:LinkButton>
</ItemTemplate>

<FooterTemplate>
     <asp:LinkButton ID="lnkbNext" CommandName="<%#PageChangedItemCommand %>" CommandArgument="<%#NextPageIndex%>"  runat="server">&gt;</asp:LinkButton>&nbsp;
     <asp:LinkButton ID="lnkbLast" CommandName="<%#PageChangedItemCommand %>" CommandArgument="<%#PagesCount%>" runat="server">&raquo;</asp:LinkButton>
</FooterTemplate>
</asp:Repeater>



public partial class Pager : System.Web.UI.UserControl
    {
        public int CurrentPage { get; set; }
        public int PageSize { get; set; }
        public int DataItemsCount { get; set; }


        private int _pagesCount;
        public int PagesCount
        {

            get
            {
                if (DataItemsCount % PageSize == 0)
                    return _pagesCount = (DataItemsCount / PageSize);

                else
                    return _pagesCount = ((DataItemsCount / PageSize) + 1);
            }

            private set
            {
                _pagesCount = value;
            }
        }

        public event EventHandler<PageChangedEventArgs> PageChanged;
        protected const string PageChangedItemCommand = "PageChanged";
        private const string CurrentPageCssStyle = "font-weight:bold; font-size:15px;";

        private void RaiseEvent(int currentPage)
        {
            if (PageChanged != null)
                PageChanged(this, new PageChangedEventArgs(currentPage));
        }

        protected List<int> DataSource
        {
            get
            {
                List<int> pages = new List<int>();
                for (int i = 1; i <= PagesCount; i++)
                    pages.Add(i);

                return pages;
            }
        }

        protected int NextPageIndex { get { return CurrentPage + 1; } }
        protected int PreviousPageIndex { get { return CurrentPage -1; } }

        public Pager()
        {
            CurrentPage = 1;
            PageSize = 1;
            DataItemsCount = 1;
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            rpt.ItemCommand += new RepeaterCommandEventHandler(rpt_ItemCommand);
            rpt.DataSource = DataSource;
            rpt.DataBind();

          //  
            if (!Page.IsPostBack)
            {
                CurrentPageSetCssStyle(CurrentPageCssStyle);
                // SetupCommandArguments();
            }
        }

        void rpt_ItemCommand(object source, RepeaterCommandEventArgs e)
        {
            if (e.CommandName == PageChangedItemCommand)
            {
                CurrentPage = int.Parse(e.CommandArgument.ToString());
                CurrentPageSetCssStyle(CurrentPageCssStyle);
               // SetupCommandArguments();
                RaiseEvent(CurrentPage);

            }
        }



        private void CurrentPageSetCssStyle(string style)
        {
            foreach (RepeaterItem item in rpt.Items)
            {
                LinkButton lnkButton = item.FindControl("p") as LinkButton;
                if (lnkButton != null)
                {
                    if (lnkButton.CommandArgument == CurrentPage.ToString())
                        lnkButton.Attributes.Add("style", style);
                }
            }
          //  SetupCommandArguments();
        }



        void SetupCommandArguments()
        {
            LinkButton lnkbPrevious = rpt.Controls[0].Controls[0].FindControl("lnkbPrevious") as LinkButton;
            if (lnkbPrevious != null)
                lnkbPrevious.CommandArgument = (CurrentPage - 1).ToString();

            LinkButton lnkbNext = rpt.Controls[rpt.Controls.Count - 1].Controls[0].FindControl("lnkbNext") as LinkButton;
            if (lnkbPrevious != null)
                lnkbPrevious.CommandArgument = (CurrentPage + 1).ToString();
        }

    }

I need to develop my own paging light control. Don't suggest me to use other paging controls.


Solution

  • I've had to do a custom pager in the last few days, but I went about it slightly differently.

    I chose to use two repeaters: 1. This one was for the paging

    <div class="searchResultsPaging" runat="server">
            <asp:Repeater ID="Paging" runat="server">
                <ItemTemplate>
                    <a href='<%# Eval("PageUrl") %>' class='<%# (Convert.ToBoolean(Eval("IsCurrent")) == true)? "pagingButtonOff" : "pagingButtonOn" %>'>
                        <%# Eval("PageText") %></a>
                </ItemTemplate>
            </asp:Repeater>
        </div>
    

    2. Was for the items I'm display

    <asp:Repeater ID="Properties" runat="server">
            <ItemTemplate>
                <div class="propertyCard">
                    <div class="propertyTitle">
                        <a href='/property/<%# Eval("Id") %>'>
                            <%# Eval("Title") %></a></div>
                    <div class="propertySuburb">
                        <%# Eval("Suburb") %></div>
                    <img src='/_Remove/SamplePropertyImages/<%# Eval("PriorityImage") %>' width="190"
                        height="143" alt='Property for sale in <%# Eval("Suburb")%>' />
                    <div class="propertyFeatures">
                        <img src="/_Remove/Icons/Bed.gif" alt='<%# Eval("Suburb") %>, <%# Eval("City") %> property has <%# Eval("Bedrooms") %> bedrooms.' /><%# Eval("Bedrooms") %><img
                            src="/_Remove/Icons/Bath.gif" alt='<%# Eval("Suburb") %>, <%# Eval("City") %> property has <%# Eval("Bathrooms") %> bedrooms.' /><%# Eval("Bathrooms") %></div>
                    <div class="propertyPrice">
                        <%# Eval("Price","{0:###,##0.00}") %>
                    </div>
                    <a href="#">View Property Details</a>
                </div>
            </ItemTemplate>
        </asp:Repeater>
    

    During binding, my business logic returned a list of properties and a total count. From there it was easy to bind and build the paging controls.

    Might not work for you since you're looking for page next/back type functionality.

    Regards, Jacqueds