ObjectDataSourceMethodEventHandler Delegate
Represents the method that will handle the Selecting, Updating, Inserting, or Deleting event of the ObjectDataSource control.
Assembly: System.Web (in System.Web.dll)
Public Delegate Sub ObjectDataSourceMethodEventHandler ( sender As Object, e As ObjectDataSourceMethodEventArgs )
Parameters
- sender
-
Type:
System.Object
The source of the event, the ObjectDataSource.
- e
-
Type:
System.Web.UI.WebControls.ObjectDataSourceMethodEventArgs
An ObjectDataSourceMethodEventArgs that contains the event data.
The Selecting, Updating, Inserting, or Deleting event of the ObjectDataSource control allows you to manipulate the parameters that are used to determine the method that is called by the ObjectDataSource control. For more information, see ObjectDataSourceMethodEventArgs.
When you create an ObjectDataSourceMethodEventHandler delegate, you identify the method that will handle the event. To associate the event with your event handler, add an instance of the delegate to the event. The event handler is called whenever the event occurs, unless you remove the delegate. For more information about event handler delegates, see NIB: Consuming Events.
This section contains two code examples. The first code example demonstrates how to use an ObjectDataSource control with a business object and a DetailsView control to insert data. The second code example provides an example middle-tier business object that is used by the first code example.
The following code example demonstrates how to use an ObjectDataSource control with a business object and a DetailsView control to insert data. Initially, the DetailsView displays a new NorthwindEmployee record, along with an automatically generated Insert button. After you enter data into the fields of the DetailsView control, click the Insert button, and the InsertMethod property will identify which method performs the Insert action.
In this example, the UpdateEmployeeInfo method is used to perform an insert; however, it requires a NorthwindEmployee parameter to insert the data. For this reason, the collection of strings that the DetailsView control passes automatically is not sufficient. The NorthwindEmployeeInserting delegate is an ObjectDataSourceMethodEventHandler object that handles the Inserting event and enables you to manipulate the input parameters before the Insert operation proceeds. Because the UpdateEmployeeInfo method requires a NorthwindEmployee object as a parameter, one is created using the collection of strings and is added to the InputParameters collection.
<%@ Register TagPrefix="aspSample" Namespace="Samples.AspNet.VB" Assembly="Samples.AspNet.VB" %> <%@ Import namespace="Samples.AspNet.VB" %> <%@ Page language="vb" %> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <script runat="server"> Private Sub NorthwindEmployeeInserting(source As Object, e As ObjectDataSourceMethodEventArgs) ' The GridView control passes an array of strings in the parameters ' collection because that is the type it knows how to work with. ' However, the business object expects a custom type. Build it ' and add it to the parameters collection. Dim paramsFromPage As IDictionary = e.InputParameters Dim ne As New NorthwindEmployee() ne.FirstName = paramsFromPage("FirstName").ToString() ne.LastName = paramsFromPage("LastName").ToString() ne.Title = paramsFromPage("Title").ToString() ne.Courtesy = paramsFromPage("Courtesy").ToString() ne.Supervisor = Int32.Parse(paramsFromPage("Supervisor").ToString()) paramsFromPage.Clear() paramsFromPage.Add("ne", ne) End Sub ' NorthwindEmployeeInserting </script> <html xmlns="http://www.w3.org/1999/xhtml" > <head> <title>ObjectDataSource - VB Example</title> </head> <body> <form id="Form1" method="post" runat="server"> <asp:detailsview id="DetailsView1" runat="server" autogenerateinsertbutton="True" datasourceid="ObjectDataSource1"> </asp:detailsview> <asp:objectdatasource id="ObjectDataSource1" runat="server" selectmethod="GetEmployee" insertmethod="UpdateEmployeeInfo" oninserting="NorthwindEmployeeInserting" typename="Samples.AspNet.VB.EmployeeLogic" > <selectparameters> <asp:parameter name="anID" defaultvalue="-1" /> </selectparameters> </asp:objectdatasource> </form> </body> </html>
The following code example provides the example middle-tier business object that the preceding code example uses. The code example consists of the following two basic classes and one additional class:
The EmployeeLogic class, which is a stateless class that encapsulates business logic.
The NorthwindEmployee class, which is a model class that contains only the basic functionality that is required to load and persist data from the data tier.
An additional NorthwindDataException class, which is provided as a convenience.
This set of example classes works with the Northwind Traders database, which is an example database that is available with Microsoft SQL Server and Microsoft Access. For a complete working example, use these classes by placing them in the App_Code directory under the application root or by compiling them into a library and placing the resulting DLL in the Bin directory. The UpdateEmployeeInfo method is not completely implemented, so you will not insert data into the Northwind Traders database when you experiment with this example.
Imports System Imports System.Collections Imports System.Configuration Imports System.Data Imports System.Data.SqlClient Imports System.Web.UI Imports System.Web.UI.WebControls Namespace Samples.AspNet.VB ' ' EmployeeLogic is a stateless business object that encapsulates ' the operations you can perform on a NorthwindEmployee object. ' Public Class EmployeeLogic ' Returns a collection of NorthwindEmployee objects. Public Shared Function GetAllEmployees() As ICollection Dim al As New ArrayList() Dim cts As ConnectionStringSettings = ConfigurationManager.ConnectionStrings("NorthwindConnection") Dim sds As New SqlDataSource(cts.ConnectionString, "SELECT EmployeeID FROM Employees") Try Dim IDs As IEnumerable = sds.Select(DataSourceSelectArguments.Empty) ' Iterate through the Enumeration and create a ' NorthwindEmployee object for each ID. Dim enumerator As IEnumerator = IDs.GetEnumerator() While enumerator.MoveNext() ' The IEnumerable contains DataRowView objects. Dim row As DataRowView = CType(enumerator.Current,DataRowView) Dim id As String = row("EmployeeID").ToString() Dim nwe As New NorthwindEmployee(id) ' Add the NorthwindEmployee object to the collection. al.Add(nwe) End While Finally ' If anything strange happens, clean up. sds.Dispose() End Try Return al End Function 'GetAllEmployees Public Shared Function GetEmployee(anID As Object) As NorthwindEmployee If anID.Equals("-1") OrElse anID.Equals(DBNull.Value) Then Return New NorthwindEmployee() Else Return New NorthwindEmployee(anID) End If End Function 'GetEmployee Public Shared Sub UpdateEmployeeInfo(ne As NorthwindEmployee) Dim retval As Boolean = ne.Save() If Not retval Then Throw New NorthwindDataException("UpdateEmployee failed.") End If End Sub 'UpdateEmployeeInfo Public Shared Sub DeleteEmployee(ne As NorthwindEmployee) Dim retval As Boolean = ne.Delete() If Not retval Then Throw New NorthwindDataException("DeleteEmployee failed.") End If End Sub 'DeleteEmployee ' And so on... End Class 'EmployeeLogic Public Class NorthwindEmployee Public Sub New() ID = DBNull.Value lastName = "" firstName = "" title = "" titleOfCourtesy = "" reportsTo = - 1 End Sub 'NewNew Public Sub New(anID As Object) Me.ID = anID Dim cts As ConnectionStringSettings = ConfigurationManager.ConnectionStrings("NorthwindConnection") Dim conn As New SqlConnection(cts.ConnectionString) Dim sc As New SqlCommand(" SELECT FirstName,LastName,Title,TitleOfCourtesy,ReportsTo " & _ " FROM Employees " & _ " WHERE EmployeeID = @empId", conn) ' Add the employee ID parameter and set its value. sc.Parameters.Add(New SqlParameter("@empId", SqlDbType.Int)).Value = Int32.Parse(anID.ToString()) Dim sdr As SqlDataReader = Nothing Try conn.Open() sdr = sc.ExecuteReader() ' Only loop once. If Not (sdr Is Nothing) AndAlso sdr.Read() Then ' The IEnumerable contains DataRowView objects. Me.aFirstName = sdr("FirstName").ToString() Me.aLastName = sdr("LastName").ToString() Me.aTitle = sdr("Title").ToString() Me.titleOfCourtesy = sdr("TitleOfCourtesy").ToString() If Not sdr.IsDBNull(4) Then Me.reportsTo = sdr.GetInt32(4) End If Else Throw New NorthwindDataException("Data not loaded for employee id.") End If Finally Try If Not (sdr Is Nothing) Then sdr.Close() End If conn.Close() Catch se As SqlException ' Log an event in the Application Event Log. Throw End Try End Try End Sub 'NewNew Private ID As Object Public ReadOnly Property EmpID() As String Get Return ID.ToString() End Get End Property Private aLastName As String Public Property LastName() As String Get Return aLastName End Get Set aLastName = value End Set End Property Private aFirstName As String Public Property FirstName() As String Get Return aFirstName End Get Set aFirstName = value End Set End Property Public ReadOnly Property FullName() As String Get Return FirstName & " " & LastName End Get End Property Private aTitle As String Public Property Title() As String Get Return aTitle End Get Set aTitle = value End Set End Property Private titleOfCourtesy As String Public Property Courtesy() As String Get Return titleOfCourtesy End Get Set titleOfCourtesy = value End Set End Property Private reportsTo As Integer Public Property Supervisor() As Integer Get Return reportsTo End Get Set reportsTo = value End Set End Property Public Function Save() As Boolean ' Implement persistence logic. Return True End Function 'Save Public Function Delete() As Boolean ' Implement delete logic. Return True End Function 'Delete End Class 'NorthwindEmployee Friend Class NorthwindDataException Inherits Exception Public Sub New(msg As String) MyBase.New(msg) End Sub 'NewNew End Class 'NorthwindDataException End Namespace
Available since 2.0