ObjectContainerDataSource

Retired Content

This content is outdated and is no longer being maintained. It is provided as a courtesy for individuals who are still using these technologies.
This page may contain URLs that were valid when originally published, but now link to sites or pages that no longer exist.

The ObjectContainerDataSource QuickStart illustrates the key usage scenarios of ObjectContainerDataSource control.

Building and Running the QuickStart

The QuickStart ships as source code, which means you must compile it before running it. This QuickStart does not require any additional setup. The following procedure describes how to build and run the QuickStart.

To build and run the QuickStart

  1. Download and extract the source code.
  2. Open the solution file ObjectContainerDataSourceQuickstart.sln.
  3. On the Build menu, click Rebuild Solution.
  4. Press F5 to run the QuickStart.

Implementation Notes

The QuickStart contains the views named CustomersSimpleView and CustomersAdvancedView. These views demonstrate common usage scenarios of the ObjectContainerDataSource control in the context of the Model-View-Presenter pattern. Both views perform select operations, update operations, delete operations, and insert operations and use the sorting and paging capabilities of the control.

CustomersSimpleView uses the default paging and sorting functionality of the ObjectContainerDataSource control and CustomersAdvancedView uses custom paging and sorting. To set the page size in CustomersSimpleView, set the page size in the control to which the ObjectContainerDataSource control is bound. In the QuickStart, it is bound to a GridView control.

The following XML code demonstrates how to set the GridView control.

<asp:GridView Width="400px" ID="GridView1" AllowSorting="True"  DataSourceID="CustomersDataSource" AutoGenerateColumns="False" EmptyDataText="No customers to display." AllowPaging="True" PageSize="4" AutoGenerateDeleteButton="True" AutoGenerateEditButton="True" DataKeyNames="Id">

For more information about paging and sorting with the ObjectContainerDataSource control, see Paging and Sorting. The two views both use an ASP.NET GridView control to display the data from the ObjectContainerDataSource control, as shown in Figure 1.

Ff709835.983b5108-770d-4451-95df-741e5a5269ff(en-us,PandP.10).png

Figure 6

ASP.NET GridView bound to an ObjectContainerDataSource control

Both views contain a property named Customers in the code-behind file. This property is set by the presenter with items that the view displays.

public IList<Customer> Customers
{
    set { CustomersDataSource.DataSource = value; }
}

The presenter for the CustomersSimpleView sets the Customers property of the view when the view loads. This means that after the view loads. it will display all available customers.

public override void OnViewLoaded()
{
    View.Customers = _controller.GetCustomers();
}

In contrast, the presenter for the CustomersAdvancedView sets the Customers property of the view in the OnSelecting event. It does this to display a page of Customer objects when the control requests the data. The OnSelecting event fires when the control requests the data for display.

public void OnSelecting(int startRowIndex, int maximumRows, string sortExpression)
{
    View.Customers = _controller.GetCustomers(startRowIndex, maximumRows, sortExpression);
    View.TotalCustomersCount = _controller.CustomersTotalCount;
}

To support update operations, delete operations, and insert operations, both views handle the OnUpdated, OnDeleted, and OnInserted events. These events must be assigned event handlers in the <ObjectContainerDataSource> tag definition. This is shown in the following XML code.

<pp:ObjectContainerDataSource ID="CustomersDataSource"  DataObjectTypeName="ObjectContainerDataSourceQuickstart.Modules.Customers.BusinessEntities.Customer" OnDeleted="CustomersDataSource_Deleted" OnInserted="CustomersDataSource_Inserted" OnUpdated="CustomersDataSource_Updated" OnSelecting="CustomersDataSource_Selecting" UsingServerPaging="True" UsingServerSorting="True" />

The views forward the events to their presenters, as shown in the following code.

protected void CustomersDataSource_Inserted(object sender, ObjectContainerDataSourceStatusEventArgs e)
{
    _presenter.OnCustomerInserted((Customer)e.Instance);
}

protected void CustomersDataSource_Deleted(object sender, ObjectContainerDataSourceStatusEventArgs e)
{
    _presenter.OnCustomerDeleted((Customer)e.Instance);
}

protected void CustomersDataSource_Updated(object sender, ObjectContainerDataSourceStatusEventArgs e)
{
    _presenter.OnCustomerUpdated((Customer)e.Instance);
}

To support custom paging and sorting, the CustomersAdvancedView contains the following additional members in the code-behind file:

  • TotalCustomersCount property. This presenter sets this property to specify the total amount of Customer objects contained in the data store. This value is forwarded to the TotalRowCount property of the ObjectContainerDataSource control. Data-bound controls use this value to render paging elements, such as page numbers.

    public int TotalCustomersCount
    {
        set { CustomersDataSource.TotalRowCount = value; }
    }
    
  • Event handler for the Selecting event. This ObjectContainerDataSource control raises this event before it performs a select operation. The view handles this event and forwards it to the presenter. In turn, the presenter sets the Customers property of the view with the Customer objects for the current page. The value of StartRowIndex is linked to the page size of the control to which the ObjectContainerDataSource control is bound.

    protected void CustomersDataSource_Selecting(object sender, ObjectContainerDataSourceSelectingEventArgs e)
    {
        _presenter.OnSelecting(e.Arguments.StartRowIndex, e.Arguments.MaximumRows, e.Arguments.SortExpression);
    }