Paging Through a Cached DataSet
As we saw earlier in this chapter, one of the downsides with the default paging functionality provided by the DataGrid is that each time a user views a different page of data, the BindData() subroutine must be called, which retrieves all the records from the database. One possible remedy of this problem is to use custom paging. Recall that custom paging can be employed to greatly reduce the amount of records being passed from the database server to the ASP.NET Web page each time the user views a different page of data.
Although custom paging can provide a substantial reduction in the amount of data being passed back and forth from the database to the ASP.NET Web page, it still has its shortcomings. Imagine that our DataSource has 40 records; displaying it in a DataGrid whose PageSize is set to 10 will result in a DataGrid with four pages. Assume that the user opts to view page 1, then page 2, and then page 1 again. With default paging, each time the user moves from viewing one page of DataGrid data to the next, 40 rows must be transmitted from the database to the ASP.NET Web page; in viewing three pages of data, 120 records must be transmitted. With custom paging, each time a user views a page, only 10 records are transmitted between the database and ASP.NET Web page. In viewing three pages, even when two of the pages viewed are the same page, a total of 30 records will be transmitted.
There are certain scenarios in which we might not need to go back to the database at all, instead using the data cache to store a DataSet populated with the database data. A specific scenario in which this would be advantageous involves displaying data that doesn't change often and is shown the same to all users. For example, imagine that an e-commerce site wants to have a page that displays the 100 top-selling items for the past week.
To allow a user to page through a cached DataSet, we can simply employ the default paging approach. The only change would be that in the BindData() subroutine, we'd bind the DataGrid to the DataSet retrieved from the cache; if the DataSet is not in the cache (either because it has been evicted, or has yet to be placed in the cache), we would need to populate the DataSet from the database and then insert it into the cache.
In the section "Caching the DataGrid's DataSource" from Chapter 7, we discussed how to cache a DataSet in the data cache. This same technique can be used to allow a user to page through a cached DataSet.
For more information on this technique, be sure to read the article ".NET Data Caching" listed in the "On the Web" section at the end of this chapter. This article discusses the .NET data cache in detail, and includes an example that illustrates how to page through a cached DataSet.