Paging and Sorting

This topic explains how to use the paging and sorting features of the ObjectContainerDataSource control. The default behavior of the ObjectContainerDataSource control is to perform the paging and sorting. It describes implementations that use the View-Presenter pattern and assumes that you are familiar with the concepts of the ObjectContainerDataSource control.
The ObjectContainerDataSource control has the following two properties that determine how sorting and paging is implemented:
  • UsingServerPaging. If this property is set to false (default value), the control pages the items it contains. This means you must add the entire collection of items to the control. If this property is set to true, you have to write code to implement paging (you add only one page of items to the control).
  • UsingServerSorting. If this property is set to false (default value), the control sorts the items it contains. If this property is set to true, you have to write code to implement sorting.

Default Paging and Sorting

By default, the ObjectContainerDataSource control performs the paging and sorting.

To configure the ObjectContainerDataSource control to perform default paging and sorting
  • In the view designer, set the UsingServerPaging property and UsingServerSorting property to false, as shown in Figure 1.
UsingServerPagingAndUsingServerSortingSetToFalse.png
Figure 1
UsingServerPaging and UsingServerSorting set to False.

To enable paging and sorting, you must also configure the data-bound control (the control that you have bound to the ObjectContainerDataSource). For example, if use a GridView, you must set the AllowSorting property and AllowPaging property to true.

Custom (Server-side) Paging and Sorting

With custom behavior, you write code to implement paging and sorting. Typically, you do this in the data access layer of your application, and the ObjectContainerDataSource control contains only a subset of items.

To configure the ObjectContainerDataSource control to perform custom paging and sorting
  • In the view designer, set the UsingServerPaging property and UsingServerSorting property to true.
UsingServerPagingAndUsingServerSortingSetToTrue.png
Figure 2
UsingServerPaging and UsingServerSorting set to True.

To sort and page items when the view loads
  • Add a public property to the view that sets the TotalRowCount property of the ObjectContainerDataSource control. When paging is performed on the server, the ObjectContainerDataSource control does not contain the entire collection of items (when paging is performed on the client application, the control does contain the entire collection of items). This property is required for the control to provide paging. This means the presenter must set the property when it handles the Selecting event of the ObjectContainerDataSource control. The following code shows the TotalRowCount property in the CustomersAdvancedView view in the ObjectContainerDataSource QuickStart.
public int TotalCustomersCount
{
    set { CustomersDataSource.TotalRowCount = value; }
} 
  • In the view, handle the Selecting event and forward it to the presenter. (The ObjectContainerDataSource raises the Selecting event when a data-bound control requests the items to display). In the following code, the view calls the presenter to handle the Selecting method.
protected void CustomersDataSource_Selecting(object sender, ObjectContainerDataSourceSelectingEventArgs e)
{
    _presenter.OnSelecting(e.Arguments.StartRowIndex, e.Arguments.MaximumRows, e.Arguments.SortExpression);
} 

The argument of the Selecting event is an instance of the ObjectContainerDataSourceSelectingEventArgs class. This class has a property named Arguments that exposes a System.Web.UI.DataSourceSelectArguments instance. You can use these arguments to retrieve information about the current select operation. The arguments you will typically use to perform custom paging and sorting are the following:
  • StartRowIndex. This argument represents the starting position you should use when retrieving objects from a data store.
  • MaximumRows. This argument represents the maximum number of objects the data source control should contain.
  • SortExpression. This argument is the sorting criteria.
Note:
If the UsingServerPaging property is set to true and the Selecting event is not handled, an InvalidOperationException is thrown.
  • In the presenter, add a method to retrieve the collection of sorted items for the current page, add the items to the view, and then set the view property for the count of total items retrieved. The following code is an example from the ObjectContainerDataSource QuickStart in the Web Client Software Factory – June 2007 release. (The controller in the QuickStart contains a simple method that sorts the results for the current page. This method is not shown here and represents code that you typically find in the data access layer of your application.)
public void OnSelecting(int startRowIndex, int maximumRows, string sortExpression)
{
    View.Customers = _controller.GetCustomers(startRowIndex, maximumRows, sortExpression);
    View.TotalCustomersCount = _controller.CustomersTotalCount;
} 

Last edited Nov 19, 2007 at 8:47 PM by siacomuzzi, version 2

Comments

No comments yet.