1

http://www.aspsnippets.com/Articles/Custom-Paging-in-ASPNet-GridView-using-SQL-Server-Stored-Procedure.aspx

Based on the tutorial above, I was able to create a custom paging on my gridview, However, I want to limit the page number to be shown on the page. Example When I have 10,000 records to display in a 10 rows per page setting, the page links will load 1 - 1000 page links which is not ideal.

How can I make the output something like this:

First 1 2 3 4 5 6 7 8 9 10 last

and adjust automatically

First 2 3 4 5 6 7 8 9 10 11 last

and so on.

Here's the code that creates the all page showing setting

private void PopulatePager(int recordCount, int currentPage)
{
    double dblPageCount = (double)((decimal)recordCount / decimal.Parse(ddlPageSize.SelectedValue));
    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));
    }
    rptPager.DataSource = pages;
    rptPager.DataBind();
}
rickyProgrammer
  • 1,177
  • 4
  • 27
  • 63

3 Answers3

8

Change you if block to this

if (pageCount > 0)
{
    int showMax = 10;
    int startPage;
    int endPage;
    if (pageCount <= showMax) 
    {
        startPage = 1;
        endPage = pageCount;
    }
    else
    {
        startPage = currentPage;
        endPage = currentPage + showMax - 1;
    }

    pages.Add(new ListItem("First", "1", currentPage > 1));

    for (int i = startPage; i <= endPage; i++)
    {
        pages.Add(new ListItem(i.ToString(), i.ToString(), i != currentPage));
    }

    pages.Add(new ListItem("Last", pageCount.ToString(), currentPage < pageCount));
}

Change showMax as necessary.

potatopeelings
  • 40,709
  • 7
  • 95
  • 119
  • This is working! Thanks :) How can I also include : Results 1-10 of (total number of record) before the pagination? – rickyProgrammer May 09 '15 at 07:50
  • 1
    You can add an asp:Label before the rptPager (referring to your link) pager control and set the text of this label based on your current page. Something like "Results " + ((currentPage - 1) * pageSize,+ 1) + "-" + Math.Min(currentPage * pageSize, recordCount) + " of " + recordCount – potatopeelings May 09 '15 at 07:54
  • But how about the total number of record? for example: 1-10 of 10,000. Anyway, thanks because you've answered the original question – rickyProgrammer May 09 '15 at 07:57
  • Sorry, I accidentally pressed enter before I was done completing my comment. Check out my (updated) earlier comment. – potatopeelings May 09 '15 at 08:54
0

Use this

Set Pager setting property

and more information use GridView Paging Sample in ASP.NET

Pradnya Bolli
  • 1,915
  • 1
  • 19
  • 37
  • This is not applicable because my datasource is directly from stored procedure, which do not support server side paging or default paging, that's why I chose custom paging. – rickyProgrammer May 09 '15 at 07:30
0

You can also add this to add exact number of pages you have

      for (int i = startPage; i <= endPage && i<dblPageCount; i++)
SaLh .A.J
  • 1
  • 2