paging in asp.net gridview using repeater control

1.9k views Asked by At

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">
1

There are 1 answers

5
Jason W On BEST ANSWER

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();
}