title: How do I use a DataPager with Server Side Paging?
body: <p>I'm trying to use a DataPager to do Server Side paging. Here is my code</p>
  
  <pre><code>&lt;asp:DataPager ID="pgrFooBars" PagedControlID="lvFooBars" 
      QueryStringField="page" runat="server" &gt;
  &lt;Fields&gt;
      &lt;asp:NumericPagerField /&gt;
  &lt;/Fields&gt;
  &lt;/asp:DataPager&gt;
  </code></pre>
  
  <p>Code Behind</p>
  
  <pre><code>protected void Page_Load(object sender, EventArgs e)
  {
      ConfigureBlogPostListView();
  }
  
  private void ConfigureBlogPostListView()
  {
      int pageNum;
      int.TryParse(Request.Params["page"], out pageNum);
      int pageSize = 20;
  
      PagedList&lt;IFooBar&gt; FooBars = FooService.GetPagedFooBars(
          new PagingSettings(pageNum, pageSize));
  
      ListViewPagedDataSource ds = new ListViewPagedDataSource();
      ds.AllowServerPaging = true;
      ds.DataSource = FooBars;
      ds.MaximumRows = pageSize;
      ds.StartRowIndex = pageNum;
      //TotalCount is the total number of records in the entire set, not just those loaded.
      ds.TotalRowCount = FooBars.TotalCount;
  
      lvFooBars.DataSource = ds;
      lvFooBars.DataBind();
  
      pgrFooBars.PageSize = pageSize;
      pgrFooBars.SetPageProperties(pageNum, FooBars.TotalCount, true);
  }
  </code></pre>
  
  <p>PagedList comes from RobConery's useful post <a href="http://blog.wekeroad.com/2007/12/10/aspnet-mvc-pagedlistt/" rel="noreferrer">http://blog.wekeroad.com/2007/12/10/aspnet-mvc-pagedlistt/</a>.</p>
  
  <p>The problem is that the DataPager appears to be using the Count property of the ListView to determine the total number of records, which in this case is 20. Somehow, it needs to know that there are 1,500, not 20 total records. The DataPager has a property TotalRowCount, but this is read-only.</p>
  
  <p>I have never seen a DataPager example with Server Side paging, but assumed that it could do Server Side Paging, otherwise what good is the QueryStringField attribute?</p>
  
  <p>I am aware that you can do a custom paging solution using methodology like the 4GuysFromRolla did here <a href="https://web.archive.org/web/20211020140032/https://www.4guysfromrolla.com/articles/031506-1.aspx" rel="noreferrer">https://web.archive.org/web/20211020140032/https://www.4guysfromrolla.com/articles/031506-1.aspx</a>, but I'd first like to know if a solution with the DataPager is possible before creating a custom solution.</p>
  
  <p><strong>UPDATE</strong>
  The more I look at this, the more that I'm coming to the conclusion that this is not possible and that, unfortunately, the datapager is a control meant for small web sites only. What I want to do should really be quite simple if the control were built correctly. I want to be able to say</p>
  
  <pre><code>dpFooBars.TotalRowCountComputed = false;
  dpFooBars.TotalRowCount = AnyNumberThatISoChoose;
  </code></pre>
  
  <p>I've been looking for some hack to accomplish the same thing, but it appears that the datapager's TotalRowCount is computed from the actual number of items in the datasource that it's bound to. It seems very odd to me that Microsoft would create a ListViewPagedDataSource() class and a DataPager at the same time and not have them work correctly together, but this appears to have been what has happened.</p>
  
  <p><strong>UPDATE 2 (AHA MOMENT?)</strong>
  It seems that it has been possible to do server side paging since .Net 2.0 by using an ObjectDataSource and customizing the SelectCountMethod(). I believe it should be possible to customize ObjectDataSource to suit my needs. Hmmm. I'm going away for the weekend, so it'll be a couple of days for me to see if this works. Stay tuned, true believers.</p>