Enhancing Paging in ListView by Using DataPager
DataPager is one of the new controls in ASP.NET 3.5, and it provides paging functionality for data-bound controls that implement the IPageableItemContainer interface, which is only the ListView control for the time being.
By default, DataPager has two commonly used paging styles:
• Numeric Pager via NumericPagerField
• Next and Previous Pager via NextPreviousPagerField
To get an idea about how to implement these basic common styles, you can read this post.
This post will explain how to enhance the paging experience in a ListView by using both a NextPreviousPagerField and a DropDownList which will enable users to select the page size of ListView. This page size is actually the PageSize property of the DataPager, and this property defines the number of records that are displayed for each page of ListView.
To avoid complexity, the following simple XML file (Contacts.xml) will be used as a data source:
And, the XMLDataSource that consumes this XML file is:
“xdsDemo” is the DataSourceID of the ListView “lvDemo”:
After getting this data binding done, LayoutTemplate of “lvDemo” should be similar to the following:
Even though it is not completely related with the topic and emphasis of this post, ItemTemplate and AltenatingItemTemplate templates of “lvDemo” are:
Finally, here is the code that sets DropDownList (ddlDemo) and DataPager (dpDemo):
As a conclusion of all these things, the result on the browser should be like:
Sample code for this demo application can be downloaded via here.