ASP.NET GridView Data Caching

Continuing my ASP.NET GridView Tips and Tricks series, here’s another one that shows how to adopt a caching mechanism in the ASP.NET GridView.

When it comes to caching data in the GridView, most developers do the mistake of caching the entire page. Now if you have an ASP.NET Page with a UserControl containing a GridView, and you have enabled OutputCaching on the entire page, then the functionality will work as expected, if the GridView does not have paging enabled.

However if the GridView has paging enabled on a page that uses OutputCaching and you try to navigate through the pages, you will find that the GridView behaves erratically.

So in order to enable caching on an ASP.NET GridView, use data caching on the DataSource. Here’s how to do it using the SqlDataSource.EnableCaching and ObjectDataSource.EnableCaching property respectively:

If GridView is bound to SQLDataSource

<asp:SqlDataSource ID="objProduct" runat="server"
ConnectionString="<%$ ConnectionStrings:NorthwindConnectionString %>"
SelectCommand="SELECT [ProductID], [ProductName] FROM [Product]"
EnableCaching="true" CacheDuration="3000" >
</
asp:SqlDataSource>

As quoted in the MSDN documentation: The SqlDataSource control supports data caching. While data is cached, the Select method retrieves data from the cache rather than from the underlying database. When the cache expires, the Select method retrieves data from the underlying database, and then caches the data again.

If GridView is bound to ObjectDataSource

<asp:ObjectDataSource ID="objProduct" runat="server" 
SelectMethod="GetProduct" SelectCountMethod="GetProductCount"
EnablePaging="True" TypeName="Product"
EnableCaching="true" CacheDuration="3000" >

As quoted in the MSDN documentation: The ObjectDataSource control supports data caching. While data is cached, calls to the Select method retrieve data from the cache rather than the ObjectDataSource creating an instance of the business object and calling its data method. When the cache expires, the Select method retrieves data from the business object, and then caches the data again.

Depending on your application, this simple trick can improve the performance of your application.

Note: The HTML markup is not cached when using this technique.

Read some more GridView Tips and Tricks over here



Will you give this article a +1 ? Thanks in advance


About The Author

Suprotim Agarwal
Suprotim Agarwal, ASP.NET Architecture MVP (Microsoft Most Valuable Professional) works as an Architect Consultant and provides consultancy on how to design and develop Web applications.

Suprotim is also the founder and primary contributor to DevCurry, DotNetCurry and SQLServerCurry. He is the Editor of a Developer Magazine called DNC Magazine. He has also written two EBooks 51 Recipes using jQuery with ASP.NET Controls. and The Absolutely Awesome jQuery CookBook

Follow him on twitter @suprotimagarwal

3 comments:

Anonymous said...

Great tip! Please share some more tips on improving gridview performance

William said...

the use of a distributed cache can also help to improve the performance and scalability of the app.

Rammohan Banda said...

Thats Great..But How can i Cache the Grid View in Such a way that when my Table data changes instead of reading from the cache page should be rendered..? I Know i can be done with Something like SqlCacheDependencies but i m not clear about it..I would be glad if you could help..