The source object for an
For an example of a source object and the ASP.NET page that includes an ObjectDataSource control that uses the source object, see ObjectDataSource Source Object Example or ObjectDataSource Strongly Typed Source Object Example.
Object Creation
The ObjectDataSource control will create an instance of the source object, call the specified method, and dispose of the object instance all within the scope of a single request, if your object has instance methods instead of static methods (Shared in Visual Basic). Therefore, your object must be stateless. That is, your object should acquire and release all required resources within the span of a single request.
You can control how the source object is created by handling the
Specifying a Class Constructor
If the source object for an ObjectDataSource control exposes public static methods (Shared in Visual Basic) that can be called to retrieve and modify data, an ObjectDataSource control will call those methods directly. If an ObjectDataSource control must create an instance of the source object in order to make method calls, the object must include a public constructor that takes no parameters. The ObjectDataSource control will call this constructor when it creates a new instance of the source object.
If the source object does not contain a public constructor without parameters, you can create an instance of the source object that will be used by the ObjectDataSource control in the ObjectCreating event, as discussed in "Object Creation," earlier in this topic.
Specifying Object Methods
The source object for an ObjectDataSource control can contain any number of methods that are used to select, insert, update, or delete data. These methods are called by the ObjectDataSource control based on the name of the method, as identified by using either the
The source object can also include an optional SelectCount method, which is identified by the ObjectDataSource control using the
Working with Strongly Typed Objects
Your source object can return and receive strongly typed objects based on the data from your data source. The ObjectDataSource control identifies the type of these objects using the
When the
-
Call the Update method from the source object that takes parameters for both current and original values.
-
Call the Delete method from the source object that takes parameters for original values.
When it uses a strongly typed object, the ObjectDataSource control will call the Update method from the source object that takes both a strongly typed object that is populated with current values and a strongly typed object that is populated with original values. However, when the ObjectDataSource control calls the Delete method and passes a strongly typed object, it calls the method that takes a single strongly typed object as a parameter, regardless of the ConflictDetection or OldValuesParameterFormatString setting. If the ConflictDetection property is set to the
Working with Designers
Optionally, you can apply attributes to your source object that describe it to a designer, such as the Microsoft Visual Web Developer Web development tool, as a data object. This can improve the experience of the developer who is using the designer by specifically identifying which methods of your object are used for select, insert, update, and delete operations.
To identify your object as a source object for an ObjectDataSource control, use the
Visual BasicВ | Copy Code |
---|---|
<DataObject(True)> _ Public Class NorthwindEmployee |
C#В | Copy Code |
---|---|
[DataObject(true)] public class NorthwindEmployee |
To identify a method as a data object method, use the
Visual BasicВ | Copy Code |
---|---|
<DataObjectMethod(DataObjectMethodType.Delete)> _ Public Shared Function DeleteEmployee(EmployeeID As Integer) As Boolean If Not _initialized Then Initialize() Dim conn As SqlConnection = New SqlConnection(_connectionString) Dim cmd As SqlCommand = New SqlCommand("DELETE FROM Employees WHERE EmployeeID = @EmployeeID", conn) cmd.Parameters.Add("@EmployeeID", SqlDbType.Int).Value = EmployeeID Try conn.Open() If cmd.ExecuteNonQuery() <> 0 Then _ Return False Catch e As SqlException ' Handle exception. Finally conn.Close() End Try Return True End Function |
C#В | Copy Code |
---|---|
[DataObjectMethod(DataObjectMethodType.Delete)] public static bool DeleteEmployee(int EmployeeID) { if (!_initialized) { Initialize(); } SqlConnection conn = new SqlConnection(_connectionString); SqlCommand cmd = new SqlCommand("DELETE FROM Employees WHERE EmployeeID = @EmployeeID", conn); cmd.Parameters.Add("@EmployeeID", SqlDbType.Int).Value = EmployeeID; try { conn.Open(); if (cmd.ExecuteNonQuery() == 0) return false; } catch (SqlException e) { // Handle exception. } finally { conn.Close(); } return true; } |