This project is read-only.
Composite Web Application Block documentation | ObjectBuilder | ContextSensitiveAutoCompleteExtender | ServerSideValidationExtender


The ASP.NET ObjectDataSource Web server control provides a way to bind data controls on a Web page to an object. Typically, the object is a middle-tier business component. The control works with the middle-tier business object to declaratively select, insert, update, delete, page, sort, cache, and filter data without extensive code.
Figure 1 illustrates the typical process that occurs when ASP.NET processes a request for a Web page that contains an ObjectDataSource. The following are the steps of the illustrated process:
  1. The data-bound control interacts with the ObjectDataSource control to retrieve or modify data.
  2. The ObjectDataSource creates an instance of the source object.
  3. The ObjectDataSource control uses reflection to call the methods to select, update, insert, and delete data. The control passes values from the bound control to the business-tier object as parameters in the method calls.
  4. The business-tier object interacts with the underlying data source to retrieve or update data.
  5. The results are returned back to the data-bound control through the ObjectDataSource control.
  6. The ObjectDataSource control disposes the business-tier object.
Figure 1
Typical request process for the ASP.NET ObjectDataSource control.

When you use the ObjectDataSource control, the Web page initiates and controls the data retrieval and display.

For more information about the ObjectDataSource control, see ObjectDataSource.

The Web Client Software Factory – June 2007 reference implementation uses the View-Presenter pattern. In this pattern, the presenter contains the logic to respond to user events (such as updating or retrieving data) and manipulates the state of the view. The ObjectContainerDataSource control implements data binding in a way that easily integrates with the View-Presenter pattern.
You can think of the ObjectContainerDataSource control as a container of objects. You programmatically populate the container, and the container raises events when changes occur to any of the contained objects. This design allows the view (Web page) to delegate the responsibility of performing select operations, update operations, delete operations, and insert operations to the presenter. Figure 2 illustrates the request process for a view that uses the ObjectContainerDataSource control in an implementation of the View-Presenter pattern.
Figure 2
Request process for the ObjectContainerDataSource control.

Using the ObjectContainerDataSource Control

The following sections describe how to use the control in your application.

Adding the Control to a Web Page

The following procedure describes how to add the control to a Web page.

To add the ObjectContainerDataSource control to a Web page
  • In Design view, open the Web page. Drag an ObjectContainerDataSource control from the Toolbox onto the Web page.
To add the ObjectContainerDataSource control in the Toolbox, right-click the Data tab in the Toolbox, and then click Choose Items. On the .NET FrameworkComponents tab, click Browse, select the Microsoft.Practices.Web.UI.WebControls.dll assembly, and then click Open. Select the ObjectContainerDataSource control in the list of components, and then click OK.
  • If the ObjectContainerDataSource Tasks shortcut menu does not appear, right-click the ObjectContainerDataSource control, and then click Show Smart Tag. On the shortcut menu, click Configure Data Source.
  • In the Configure Data Source dialog box, select the data object type. If you do not see the type in the list of available types, you must add a reference (to the Web site) to the assembly that contains the type definition. Consider the following requirements for the object type:
    • If you want to perform update operations, delete operations, or insert operations, the type you select must have a constructor with no parameters.
    • If you want to perform update operations or delete operations, the type must have a property that uniquely identifies each instance of that type. This property is the key for the collection of items in the ObjectContainerDataSource control. The control supports composite keys. This means you can use more than one property to form a key for a particular object type.
  • Click OK. The dialog box sets the DataObjectTypeName property of the ObjectContainerDataSource control to the name of the selected type.
  • Add a data-bound control to the page and set its DataSourceID property to the ID of the data source control.

Adding Objects to the Control

At run time, the data-bound control on a Web page displays the objects contained in the ObjectContainerDataSource control. To display objects, you must add them to the ObjectContainerDataSource control. To add them, you set the DataSource property of the control to one of the following objects:
  • A single data object of the type you set in the configuration wizard
  • An object that implements the IEnumerable interface whose items are of the type you set in the DataObjectTypeName property
If you attempt to add objects that do not match the type defined in the Configure Data Source dialog box, the ObjectContainerDataSource control will throw an exception of type InvalidOperationException.

The following procedure describes how to add items to the ObjectContainerDataSource control when a view loads.

To show items when the view loads
  • Add a public property to the view. In this property, add items to the ObjectContainerDataSource control using the DataSource property. The presenter uses this property to populate the control. The following code example is taken from the Web Client Software Factory – June 2007 ObjectContainerDataSource QuickStart and shows the Customers property in the CustomersSimpleView.aspx.cs file.
public IList<Customer> Customers
    set { ObjectContainerDataSource1.DataSource = value; }
  • In the OnViewLoaded method of the presenter, use the property you added in the previous step to add items to the control.
public override void OnViewLoaded()
    View.Customers = this._controller.GetCustomers();

This code is taken from the CustomersSimpleViewPresenter class in the ObjectContainerDataSource QuickStart. This method is called by the view every time it loads. The presenter calls the GetCustomers method of the controller to retrieve all the customer instances to display and sets the Customers property of the view.

Performing Update Operations, Insert Operations, and Delete Operations

When the user performs an update operations, insert operation, or delete operation in a data-bound control, the data source control raises the following events.

public event EventHandler<ObjectContainerDataSourceStatusEventArgs> Updated;
public event EventHandler<ObjectContainerDataSourceStatusEventArgs> Inserted;
public event EventHandler<ObjectContainerDataSourceStatusEventArgs> Deleted; 

To reflect the changes made by the user in the underlying data store of your application, you must handle these events and perform the required actions to update the data store.

To perform update operations, insert operations, and delete operations
  • In Designer view, open the Web page that contains the control. In the Properties window for the ObjectContainerDataSource control, display the events for the control, and then specify methods for the OnDeleted event, OnInserted event, and OnUpdated event. When you save the file, Visual Studio updates the declaration on the Web page. The following declaration is an example from the ObjectContainerDataSource QuickStart.
<pp:ObjectContainerDataSource ID="CustomersDataSource" runat="server" DataObjectTypeName="ObjectContainerDataSourceQuickstart.Modules.Customers.BusinessEntities.Customer" OnDeleted="CustomersDataSource_Deleted" OnInserted="CustomersDataSource_Inserted" OnUpdated="CustomersDataSource_Updated" OnSelecting="CustomersDataSource_Selecting" UsingServerPaging="True" UsingServerSorting="True" /> 
  • Implement the event handlers in your view. In the event handlers, call methods the presenter to handle the operations. The following code contains event handlers from the ObjectContainerDataSource QuickStart (_presenter is a member variable that is the presenter for the view).
protected void CustomersDataSource_Inserted(object sender, ObjectContainerDataSourceStatusEventArgs e)

protected void CustomersDataSource_Deleted(object sender, ObjectContainerDataSourceStatusEventArgs e)

protected void CustomersDataSource_Updated(object sender, ObjectContainerDataSourceStatusEventArgs e)

The ObjectContainerDataSourceStatusEventArgs class contains the following members:
  • Instance. This is the data object. The ObjectContainerDataSource control uses reflection to create this object (on every update operation, delete operation, and insert operation) and populates it with the input data from the data-bound control.
  • AffectedRows. This is an integer that contains the number of rows affected in the internal data store of the ObjectContainerDataSource.
To support insert operations, delete operations, and update operations, the class of the data object must have a constructor that takes no parameters.

The data source control uses the data object type's key properties to uniquely identify an object. To enable update operations and delete operations, you must specify the names of the key properties of your data object in the data-bound control that consumes the data source control. For example, if you are using a GridView control, you must set the DataKeyNames property.

Sorting and Paging

The ObjectContainerDataSource control supports two ways of sorting and paging the items it contains:
  • Default paging and sorting. The control performs paging and sorting. With this approach, the control typically contains the entire collection of items.
  • Custom (server-side) paging and sorting. The developer must write code to page and sort the items. With this approach, the control typically contains a subset of items.
For more information about sorting and paging with the ObjectContainerDataSource control, see Paging and Sorting.

Composite Web Application Block documentation | ObjectBuilder | ContextSensitiveAutoCompleteExtender | ServerSideValidationExtender

Last edited Nov 19, 2007 at 8:59 PM by siacomuzzi, version 6


No comments yet.