0

I am using asp.net gridview and for paging I am using repeater control.

But my paging looks like this

enter image description here

What I want is that it should look like this

enter image description here

My code where I am populating my pager is given below

private void PopulatePager(int recordCount, int currentPage)
{
    double dblPageCount = (double)((decimal)recordCount / decimal.Parse((1).ToString()));
    int pageCount = (int)Math.Ceiling(dblPageCount);
    List<ListItem> pages = new List<ListItem>();
    if (pageCount > 0)
    {
        pages.Add(new ListItem("First", "1", currentPage > 1));
        for (int i = 1; i <= pageCount; i++)
        {
            pages.Add(new ListItem(i.ToString(), i.ToString(), i != currentPage));
        }
        pages.Add(new ListItem("Last", pageCount.ToString(), currentPage < pageCount));
    }
  //  aa.Controls.Add(
    System.Web.UI.HtmlControls.HtmlGenericControl g = new System.Web.UI.HtmlControls.HtmlGenericControl();


    rptPager.DataSource = pages;
    rptPager.DataBind();
}

Any help will be highly appreciated

My repeater control markup is following

   <ul class="pagination">
       <li>
        <asp:LinkButton CssClass="pagination"   ID="lnkPage" runat="server" Text = '<%#Eval("Text") %>' CommandArgument = '<%# Eval("Value") %>' Enabled = '<%# Eval("Enabled") %>' OnClick = "Page_Changed"></asp:LinkButton>
       </li> </ul>
    </ItemTemplate>
    </asp:Repeater>
    <ul id="aa"  runat="server" class="pagination">
fc123
  • 898
  • 3
  • 16
  • 40

1 Answers1

2

You could change the page population to add "..." before and/or after the listed page numbers, and then have logic to only do so if you can't see the first or last page for the range of pages you wish to show. Provided the currentPage and pageCount are accurate, the code below should generate the appropriate page list items for the desired range of pages to show, in this case, 4.

int pagesToShow = 4;
int minPage = Math.Max(1, currentPage - (pagesToShow / 2));
int maxPage = Math.Min(pageCount, minPage + pagesToShow);
if (minPage > 1)
    pages.Add(new ListItem("...", (minPage - 1).ToString(), false));
for (int i = minPage; i <= maxPage; i++)
{
    pages.Add(new ListItem(i.ToString(), i.ToString(), i != currentPage));
}
if (maxPage < pageCount)
    pages.Add(new ListItem("...", (maxPage + 1).ToString(), false));

EDIT So your final method becomes:

private void PopulatePager(int recordCount, int currentPage)
{
    double dblPageCount = (double)((decimal)recordCount / decimal.Parse((1).ToString()));
    int pageCount = (int)Math.Ceiling(dblPageCount);
    List<ListItem> pages = new List<ListItem>();
    if (pageCount > 0)
    {
        pages.Add(new ListItem("First", "1", currentPage > 1));
        int pagesToShow = 4;
        int minPage = Math.Max(1, currentPage - (pagesToShow / 2));
        int maxPage = Math.Min(pageCount, minPage + pagesToShow);
        if (minPage > 1)
            pages.Add(new ListItem("...", (minPage - 1).ToString(), false));
        for (int i = minPage; i <= maxPage; i++)
            pages.Add(new ListItem(i.ToString(), i.ToString(), i != currentPage));
        if (maxPage < pageCount)
            pages.Add(new ListItem("...", (maxPage + 1).ToString(), false));
        pages.Add(new ListItem("Last", pageCount.ToString(), currentPage < pageCount));
    }
    System.Web.UI.HtmlControls.HtmlGenericControl g = new System.Web.UI.HtmlControls.HtmlGenericControl();
    rptPager.DataSource = pages;
    rptPager.DataBind();
}
Jason W
  • 13,026
  • 3
  • 31
  • 62
  • Can u please change the variable names to the ones that I used in my code? – fc123 Nov 25 '14 at 05:06
  • I also need first and last page link – fc123 Nov 25 '14 at 05:08
  • The variable names do work with your existing code. The code only depends on your existing `pages, pageCount, currentPage` variables. There is nothing wrong with your first and last page links, so just replace your `for loop` between the first and last page links (keeping those) with the code above, and it should work. – Jason W Nov 25 '14 at 05:14
  • Ok understood: I should replace my for loop. And add "if (minPage > 1)" and "if (maxPage < pageCount)". Is that right? are we on same page? – fc123 Nov 25 '14 at 05:35
  • 1
    Yes, that is correct. I updated the answer to show how to integrate what I am suggesting. – Jason W Nov 25 '14 at 05:39