Represents a business object that provides data to data-bound controls in multitier Web application architectures.
See Also: ObjectDataSource Members
In this topic:
An System.Web.UI.WebControls.ObjectDataSource control works with a class that you create. You create methods that retrieve and update data, and you provide the names of those methods to the System.Web.UI.WebControls.ObjectDataSource control in markup. During rendering or postback processing, the System.Web.UI.WebControls.ObjectDataSource calls the methods that you have specified.
There is no visual rendering of the System.Web.UI.WebControls.ObjectDataSource control. As a result, the System.Web.UI.WebControls.ObjectDataSource does not support visual features such as the System.Web.UI.DataSourceControl.EnableTheming or System.Web.UI.DataSourceControl.SkinID property.
A very common application design practice is to separate the presentation layer from business logic and to encapsulate the business logic in business objects. These business objects form a distinct layer between the presentation layer and the data tier, resulting in a three-tier application architecture. The System.Web.UI.WebControls.ObjectDataSource control enables developers to use an ASP.NET data source control while retaining their three-tier application architecture.
The System.Web.UI.WebControls.ObjectDataSource control uses reflection to create instances of business objects and to call methods on them to retrieve, update, insert, and delete data. The ObjectDataSource.TypeName property identifies the name of the class that the System.Web.UI.WebControls.ObjectDataSource works with. The System.Web.UI.WebControls.ObjectDataSource control creates and destroys an instance of the class for each method call; it does not hold the object in memory for the lifetime of the Web request. This is a serious consideration if the business object that you use requires many resources or is otherwise expensive to create and destroy. Using an expensive object might not be an optimal design choice, but you can control the life cycle of the object by using the ObjectDataSource.ObjectCreating, ObjectDataSource.ObjectCreated, and ObjectDataSource.ObjectDisposing events.
The methods that are identified by the ObjectDataSource.SelectMethod, ObjectDataSource.UpdateMethod, ObjectDataSource.InsertMethod, and ObjectDataSource.DeleteMethod properties can be instance methods or static (Shared in Visual Basic) methods. If the methods are static (Shared in Visual Basic), an instance of the business object is not created, and the ObjectDataSource.ObjectCreating, ObjectDataSource.ObjectCreated, and ObjectDataSource.ObjectDisposing events are not raised.
To retrieve data from a business object, set the ObjectDataSource.SelectMethod property to the name of the method that retrieves data. If the method does not return an IEnumerable or System.Data.DataSet object, the object is wrapped by the runtime in an IEnumerable collection. If the method signature has parameters, you can add System.Web.UI.WebControls.Parameter objects to the ObjectDataSource.SelectParameters collection, and then bind them to the values that you want to pass to the method that is specified by the ObjectDataSource.SelectMethod property. In order for the System.Web.UI.WebControls.ObjectDataSource control to use the parameters, the parameters must match the names and types of the parameters in the method signature. For more information, see Using Parameters with the ObjectDataSource Control.
The System.Web.UI.WebControls.ObjectDataSource control retrieves data whenever the ObjectDataSource.Select method is called. This method provides programmatic access to the method that is specified by ObjectDataSource.SelectMethod property. The method that is specified by the ObjectDataSource.SelectMethod property is called automatically by controls that are bound to the System.Web.UI.WebControls.ObjectDataSource when their DataBind method is called. If you set the DataSourceID property of a data-bound control, the control automatically binds to data from the data source, as needed. Setting the DataSourceID property is the recommended method for binding an System.Web.UI.WebControls.ObjectDataSource control to a data-bound control. Alternatively, you can set the DataSource property, but then you must explicitly call the DataBind method of the data-bound control. You can call the ObjectDataSource.Select method programmatically at any time to retrieve data.
For more information about binding data-bound controls to data source controls, see Binding to Data Using a Data Source Control.
Depending on the capabilities of the business object that the System.Web.UI.WebControls.ObjectDataSource control works with, you can perform data operations, such as updates, insertions, and deletions. To perform these data operations, set the appropriate method name and any associated parameters for the operation that you want to perform. For example, for an update operation, set the ObjectDataSource.UpdateMethod property to the name of the business object method that performs updates and add any required parameters to the ObjectDataSource.UpdateParameters collection. If the System.Web.UI.WebControls.ObjectDataSource control is associated with a data-bound control, the parameters are added by the data-bound control. In this case, you need to ensure that the parameter names of the method match the field names in the data-bound control. The update is performed when the ObjectDataSource.Update method is called, either explicitly by your code or automatically by a data-bound control. The same general pattern is followed for ObjectDataSource.Delete and ObjectDataSource.Insert operations. Business objects are assumed to perform these types of data operations one record at a time, rather than batched.
The System.Web.UI.WebControls.ObjectDataSource control can filter data that is retrieved by the ObjectDataSource.SelectMethod property, if the data is returned as a System.Data.DataSet or System.Data.DataTable object. You can set the ObjectDataSource.FilterExpression property to a filtering expression by using a format string syntax and bind values in the expression to parameters that are specified in the ObjectDataSource.FilterParameters collection.
Although the System.Web.UI.WebControls.ObjectDataSource does not retain the instance of the business object across multiple requests, it can cache the result of calling the method identified by the ObjectDataSource.SelectMethod property. While the data is cached, subsequent calls to the ObjectDataSource.Select method return the cached data instead of creating the business object and calling its ObjectDataSource.SelectMethod using reflection. Caching lets you avoid creating the object and calling its data method at the expense of memory on the Web server. The System.Web.UI.WebControls.ObjectDataSource automatically caches data when the ObjectDataSource.EnableCaching property is set to true, and the ObjectDataSource.CacheDuration property is set to the number of seconds that the cache stores data before the cache is discarded. You can also specify a ObjectDataSource.CacheExpirationPolicy property and an optional ObjectDataSource.SqlCacheDependency property. The System.Web.UI.WebControls.ObjectDataSource control allows you to cache all types of data, but you should not cache objects that retain resources or state that cannot be shared to service multiple requests (for example, an open System.Data.SqlClient.SqlDataReader object), because the same instance of the object will be used to service multiple requests.
The following table describes the features of the System.Web.UI.WebControls.ObjectDataSource control.
Set the ObjectDataSource.SelectMethod property to the name of the business object method that selects data, and include any necessary parameters in the ObjectDataSource.SelectParameters collection either programmatically or by using a data-bound control.
Set the ObjectDataSource.FilterExpression property to a filtering expression and optionally add any parameters to the ObjectDataSource.FilterParameters collection to filter the data when the ObjectDataSource.Select method is called. The method specified by the ObjectDataSource.SelectMethod property must return a System.Data.DataSet or System.Data.DataTable.
Data source paging is supported, if the ObjectDataSource.SelectMethod method contains parameters for the maximum number of records to retrieve and the index of the first record to retrieve. The names of those parameters must be set in the ObjectDataSource.MaximumRowsParameterName and ObjectDataSource.StartRowIndexParameterName properties, respectively. A data-bound control might be able to perform paging itself, even if the System.Web.UI.WebControls.ObjectDataSource control does not support paging directly in the method specified by the ObjectDataSource.SelectMethod property. The requirement for the data-bound control to be able to do this is that the method specified by the ObjectDataSource.SelectMethod property return an object that implements the ICollection interface.
Set the ObjectDataSource.DeleteMethod property to the name of the business object method or function that deletes data, and include any necessary parameters in the ObjectDataSource.DeleteParameters collection.
Set the ObjectDataSource.InsertMethod property to the name of the business object method or function that inserts data, and include any necessary parameters in the ObjectDataSource.InsertParameters collection.
Set the ObjectDataSource.EnableCaching property to true, and the ObjectDataSource.CacheDuration and ObjectDataSource.CacheExpirationPolicy properties according to the caching behavior you want for your cached data.
When you use the System.Web.UI.WebControls.ObjectDataSource class to update or insert data, strings that are entered at the client are not automatically converted from the client culture format to the server culture format. For example, the client culture might specify DD/MM/YYYY as the date format, and the date format on the server might be MM/DD/YYYY. In that case, October 5, 2009 would be entered in a System.Web.UI.WebControls.TextBox control as 5/10/2009 but would be interpreted as May 10, 2009. October 15, 2009 would be entered as 15/10/2009, and would be rejected as an invalid date.
As with all data source controls, the System.Web.UI.WebControls.ObjectDataSource control is associated with a data source view class. While the System.Web.UI.WebControls.ObjectDataSource control is the interface that the page developer uses to work with data, the System.Web.UI.WebControls.ObjectDataSourceView class is the interface that data-bound controls work with. Additionally, the System.Web.UI.WebControls.ObjectDataSourceView class describes the capabilities of the data source control, and performs the actual work. The System.Web.UI.WebControls.ObjectDataSource control has only one associated System.Web.UI.WebControls.ObjectDataSourceView, and it is always named DefaultView. While the System.Web.UI.WebControls.ObjectDataSourceView object is exposed by the ObjectDataSource.GetView(string) method, many of its properties and methods are wrapped and exposed directly by the System.Web.UI.WebControls.ObjectDataSource control. Behind the scenes, the System.Web.UI.WebControls.ObjectDataSourceView object performs all data operations, including retrieving, inserting, updating, deleting, filtering, and sorting the data. For more information, see System.Web.UI.WebControls.ObjectDataSourceView.
You can use the System.Web.UI.WebControls.ObjectDataSource control with a LINQ to SQL class. To do so, you set the ObjectDataSource.TypeName property to the name of the data-context class. You also set the ObjectDataSource.SelectMethod, ObjectDataSource.UpdateMethod, ObjectDataSource.InsertMethod, and ObjectDataSource.DeleteMethod methods to the methods in the data-context class that perform the corresponding operations. You must create an event handler for the ObjectDataSource.ObjectDisposing event in order to cancel disposing of the data-context class. This step is necessary because LINQ to SQL supports deferred execution, whereas the System.Web.UI.WebControls.ObjectDataSource control tries to dispose the data context after the Select operation. For more information about how to create LINQ to SQL classes, see How to: Create LINQ to SQL Classes in a Web Application. For an example of how to cancel the disposing of a data context class, see the ObjectDataSource.ObjectDisposing event.