title: Limit the page numbers to show on Custom Paging
body: <p><a href="http://www.aspsnippets.com/Articles/Custom-Paging-in-ASPNet-GridView-using-SQL-Server-Stored-Procedure.aspx" rel="nofollow">http://www.aspsnippets.com/Articles/Custom-Paging-in-ASPNet-GridView-using-SQL-Server-Stored-Procedure.aspx</a></p>
  
  <p>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.</p>
  
  <p>How can I make the output something like this:</p>
  
  <p><strong>First 1 2 3 4 5 6 7 8 9 10 last</strong></p>
  
  <p>and adjust automatically</p>
  
  <p><strong>First 2 3 4 5 6 7 8 9 10 11 last</strong></p>
  
  <p>and so on.</p>
  
  <p>Here's the code that creates the all page showing setting</p>
  
  <pre><code>private void PopulatePager(int recordCount, int currentPage)
  {
      double dblPageCount = (double)((decimal)recordCount / decimal.Parse(ddlPageSize.SelectedValue));
      int pageCount = (int)Math.Ceiling(dblPageCount);
      List&lt;ListItem&gt; pages = new List&lt;ListItem&gt;();
      if (pageCount &gt; 0)
      {
          pages.Add(new ListItem("First", "1", currentPage &gt; 1));
          for (int i = 1; i &lt;= pageCount; i++)
          {
              pages.Add(new ListItem(i.ToString(), i.ToString(), i != currentPage));
          }
          pages.Add(new ListItem("Last", pageCount.ToString(), currentPage &lt; pageCount));
      }
      rptPager.DataSource = pages;
      rptPager.DataBind();
  }
  </code></pre>