Programming Microsoft ASP.NET 4 - Dino Esposito [242]
Programming Interface of ObjectDataSource
The ObjectDataSource component provides nearly the same programmatic interface (events, methods, properties, and associated behaviors) as the SqlDataSource, with the addition of three new events and a few properties. The events the ObjectDataSource fires are related to the lifetime of the underlying business object the ObjectDataSource is bound to—ObjectCreating, ObjectCreated, and ObjectDisposing. Table 10-19 lists other key properties of ObjectDataSource.
Table 10-19. Main Properties of ObjectDataSource
Property
Description
ConvertNullToDBNull
DataObjectTypeName
Indicates whether null parameters passed to insert, delete, or update operations are converted to System.DBNull. This property is set to false by default.
Gets or sets the name of a class that is to be used as a parameter for a select, insert, update, or delete operation.
DeleteMethod, DeleteParameters
Gets or sets the name of the method and related parameters used to perform a delete operation.
EnablePaging
Indicates whether the control supports paging.
FilterExpression, FilterParameters
Indicates the filter expression (and parameters) to filter the output of a select operation.
InsertMethod, InsertParameters
Gets or sets the name of the method and related parameters used to perform an insert operation.
MaximumRowsParameterName
If the EnablePaging property is set to true, indicates the parameter name of the Select method that accepts the value for the number of records to retrieve.
OldValuesParameterFormatString
Gets or sets a format string to apply to the names of any parameters passed to the Delete or Update methods.
SelectCountMethod
Gets or sets the name of the method used to perform a select count operation.
SelectMethod, SelectParameters
Gets or sets the name of the method and related parameters used to perform a select operation.
SortParameterName
Gets or sets the name of an input parameter used to sort retrieved data. It raises an exception if the parameter is missing.
StartRowIndexParameterName
If the EnablePaging property is set to true, indicates the parameter name of the Select method that accepts the value for the starting record to retrieve.
UpdateMethod, UpdateParameters
Gets or sets the name of the method and related parameters used to perform an update operation.
The ObjectDataSource control uses reflection to locate and invoke the method to handle the specified operation. The TypeName property returns the fully qualified name of the assembly that defines the class to call.
Implementing Data Retrieval
The following code snippet illustrates a class that can be used with an object data source. In the example, the class does not use LINQ-to-SQL or Entity Framework; it is instead based on plain ADO.NET code. You can easily rewrite it to perform data access via the context of LINQto-SQL or Entity Framework. The Employee class being used is assumed to be a custom class created just to simplify data manipulation.
public class EmployeeRepository
{
public static string ConnectionString {
...
}
public static void Load(int employeeID) {
...
}
public static IList ... } public static IList ... } public static void Save(Employee emp) { ... } public static void Insert(Employee emp) { ... } public static void Delete(int employeeID) { ... } ... } If you don’t use static methods, the worker class you