PagerDataSource using a Data Source and custom paging
This sample shows how to use the PagerDataSource to obtain data from a custom data source in order to populate a DataList.
In this sample we'll get the data directly from our data mapper, using a method
that retrieves the data needed for the current page (the method is GetPagedData(int
startIndex, int maxRows)). To see an example where we use a method that retrieves
all the data from a data source choose the sample "Paging a
Data Source (1)".
The PagerDataSource control acts as a data source and as a data
consumer. The DataList will consume the data exposed by the
PagerDataSource (the data of the current page), and the PagerDataSource will consume
the data retrieved from the data mapper.
If the PagerDataSource is not bound to a DataSourceControl, it
can not exploit the high degree of integration
obtained by using the new ASP.NET
2.0 DataSourceControls, so we have to write some code in order to provide the data
to the control.
When the PagerDataSource changes the page to show (and the first time
it executes), it will raise the PageChanged event. If the PagerDataSource
is bound to a DataSourceControl (using the DataSourceID property), it will fetch
the data from the DataSourceControl automatically. However, if the control is not
bound to a DataSourceControl, we have do some things in order to provide the data
for the current page:
- As we are going to bind only the current page, we have to set the property
CustomPaging property to "true" in the PagerDataSource.
- Set the DataSource
property with the data for the current page. In order to know which items to retrieve we can use the properties StartIndex and MaxRows from the
PageChangedEventArgs instance passed to the PageChanged
event.
- Call the DataBind method.
- We have to set the total item count (using the TotalItemCount
property present in the PageChangedEventArgs instance passed to the PageChanged
event) so the PagerDataSource
will find out the total number of pages to show.
The source code for this page is shown after the live sample.
Product List
|
1
|
|
Product 1
|
|
Full description for product number 1
|
|
2.00 $
|
|
|
2
|
|
Product 2
|
|
Full description for product number 2
|
|
4.00 $
|
|
|
3
|
|
Product 3
|
|
Full description for product number 3
|
|
6.00 $
|
|
|
4
|
|
Product 4
|
|
Full description for product number 4
|
|
8.00 $
|
|
|
5
|
|
Product 5
|
|
Full description for product number 5
|
|
10.00 $
|
|
|
6
|
|
Product 6
|
|
Full description for product number 6
|
|
12.00 $
|
|
|
7
|
|
Product 7
|
|
Full description for product number 7
|
|
14.00 $
|
|
|
8
|
|
Product 8
|
|
Full description for product number 8
|
|
16.00 $
|
|
|
9
|
|
Product 9
|
|
Full description for product number 9
|
|
18.00 $
|
|
|
10
|
|
Product 10
|
|
Full description for product number 10
|
|
20.00 $
|
|
|
11
|
|
Product 11
|
|
Full description for product number 11
|
|
22.00 $
|
|
|
12
|
|
Product 12
|
|
Full description for product number 12
|
|
24.00 $
|
|
|
13
|
|
Product 13
|
|
Full description for product number 13
|
|
26.00 $
|
|
|
14
|
|
Product 14
|
|
Full description for product number 14
|
|
28.00 $
|
|
|