Implementing Outputcache with ListView Paging

The ListView Control Supports a lot of functionalities , one of those functionalities is the built in support for data paging .

You can implement data paging in ListView with a help of the  DataPager control.

The default behavior of the DataPager control is to provide paging functionality through PostBacks(like the GridView ) control , but the interesting thing is that the Pager Can Be configured to use the query string to keep the current displayed page , this behavior is very important if it used with the output cache to vary the page output Based on That query Field, this way you will have a separate cached version of the web page  for each ListView Page, so that you can Provide an Effective and scalable data paging solution ( for example like the "http://weblogs.asp.net" page ) .

To configure the DataPager control to use the querystring , you need to set its "QueryStringField" property to the query string paramter name that you want to use .

<asp:DataPager ID="DataPager1" runat="server" QueryStringField="PageNumber"....

 

Now the Pager control will add the page number to the querystring field in the page url , for example :

Default.aspx?PageNumber=1

 

Now you can vary the page outputcache based on the "PageNumber" query string field using output cache directive , in the page decalration :

<%@ OutputCache Duration="90" VaryByParam="PageNumber"  %>

 

This way you will have a separate cached version for each ListView page , and so your application will use the cached version for the subsequent requsts of the same ListViewPage.

You can also optimize this solution by turning off the ListView ViewState by setting its EnabeViewState property to false .

 

Anas Ghanem

1 Comment

  • I've noticed that when I set the QueryStringField attribute on the DataPager control within a web site which uses cookiess sessions (i.e. the sessionID is rewritten into the URL) the URL generate for Next page, previous page etc. do not work because they are absolute URLs. This is surely a bug with the control...?

Comments have been disabled for this content.