ObjectDataSourceView.OnDeleting(ObjectDataSourceMethodEventArgs) 方法

定义

Deleting 对象尝试执行删除操作之前引发 ObjectDataSourceView 事件。Raises the Deleting event before the ObjectDataSourceView object attempts a delete operation.

protected:
 virtual void OnDeleting(System::Web::UI::WebControls::ObjectDataSourceMethodEventArgs ^ e);
protected virtual void OnDeleting (System.Web.UI.WebControls.ObjectDataSourceMethodEventArgs e);
abstract member OnDeleting : System.Web.UI.WebControls.ObjectDataSourceMethodEventArgs -> unit
override this.OnDeleting : System.Web.UI.WebControls.ObjectDataSourceMethodEventArgs -> unit
Protected Overridable Sub OnDeleting (e As ObjectDataSourceMethodEventArgs)

参数

e
ObjectDataSourceMethodEventArgs

包含事件数据的 ObjectDataSourceMethodEventArgsAn ObjectDataSourceMethodEventArgs that contains the event data.

示例

下面的代码示例演示如何将ObjectDataSource控件与业务对象GridView和控件结合使用来删除数据。The following code example demonstrates how to use an ObjectDataSource control with a business object and a GridView control to delete data. 最初使用由属性指定的方法从EmployeeLogic对象中检索数据, 从而显示一组所有雇员。 SelectMethod GridViewThe GridView initially displays a set of all employees, using the method that is specified by the SelectMethod property to retrieve the data from the EmployeeLogic object. 由于属性设置为true, 因此该控件会GridView自动显示 "删除" 按钮。 AutoGenerateDeleteButtonBecause the AutoGenerateDeleteButton property is set to true, the GridView control automatically displays a Delete button.

如果单击 "删除" 按钮, 则Delete将使用由DeleteMethod属性指定的方法和DeleteParameters集合中指定的任何参数执行该操作。If you click the Delete button, the Delete operation is performed using the method that is specified by the DeleteMethod property and any parameters that are specified in the DeleteParameters collection. 在此代码示例中, 还执行了一些预处理和后处理步骤。In this code example, some preprocessing and post-processing steps are also performed. NorthwindEmployeeDeleting执行Delete NorthwindEmployeeDeleted Deleted操作之前Deleting调用委托来处理事件, 并在操作完成后调用委托来处理事件。 Delete执行异常处理。The NorthwindEmployeeDeleting delegate is called to handle the Deleting event before the Delete operation is performed, and the NorthwindEmployeeDeleted delegate is called to handle the Deleted event after the Delete operation has completed, to perform an exception handling. 在此示例中, 如果NorthwindDataException引发了, 则由该委托处理。In this example, if a NorthwindDataException is thrown, it is handled by this delegate.

若要检查此代码示例EmployeeLogic使用的中间层业务对象的实现, 请参见。 ObjectDataSourceStatusEventArgsTo examine the implementation of the EmployeeLogic middle-tier business object that this code example uses, see ObjectDataSourceStatusEventArgs.

<%@ Register TagPrefix="aspSample" Namespace="Samples.AspNet.CS" Assembly="Samples.AspNet.CS" %>
<%@ Import namespace="Samples.AspNet.CS" %>
<%@ Page language="c#" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<script runat="server">
private void NorthwindEmployeeDeleting(object source, ObjectDataSourceMethodEventArgs e)
{
  // The GridView passes the ID of the employee
  // to be deleted. However, the buisiness object, EmployeeLogic,
  // requires a NorthwindEmployee parameter, named "ne". Create
  // it now and add it to the parameters collection.
  IDictionary paramsFromPage = e.InputParameters;
  if (paramsFromPage["EmpID"] != null) {
    NorthwindEmployee ne
      = new NorthwindEmployee( Int32.Parse(paramsFromPage["EmpID"].ToString()));
    // Remove the old EmpID parameter.
    paramsFromPage.Clear();
    paramsFromPage.Add("ne", ne);
  }
}

private void NorthwindEmployeeDeleted(object source, ObjectDataSourceStatusEventArgs e)
{
  // Handle the Exception if it is a NorthwindDataException
  if (e.Exception != null)
  {

    // Handle the specific exception type. The ObjectDataSource wraps
    // any Exceptions in a TargetInvokationException wrapper, so
    // check the InnerException property for expected Exception types.
    if (e.Exception.InnerException is NorthwindDataException)
    {
      Label1.Text = e.Exception.InnerException.Message;
      // Because the exception is handled, there is
      // no reason to throw it.
      e.ExceptionHandled = true;
    }
  }
}

</script>
<html xmlns="http://www.w3.org/1999/xhtml" >
  <head>
    <title>ObjectDataSource - C# Example</title>
  </head>
  <body>
    <form id="Form1" method="post" runat="server">

        <asp:gridview
          id="GridView1"
          runat="server"
          datasourceid="ObjectDataSource1"
          autogeneratedeletebutton="true"
          autogeneratecolumns="false"
          datakeynames="EmpID">
          <columns>
            <asp:boundfield headertext="EmpID" datafield="EmpID" />
            <asp:boundfield headertext="First Name" datafield="FirstName" />
            <asp:boundfield headertext="Last Name" datafield="LastName" />
          </columns>
        </asp:gridview>

        <asp:objectdatasource
          id="ObjectDataSource1"
          runat="server"
          selectmethod="GetAllEmployees"
          deletemethod="DeleteEmployee"
          ondeleting="NorthwindEmployeeDeleting"
          ondeleted="NorthwindEmployeeDeleted"
          typename="Samples.AspNet.CS.EmployeeLogic">
          <deleteparameters>
            <asp:parameter name="EmpID" type="Int32" />
          </deleteparameters>
        </asp:objectdatasource>

        <asp:label id="Label1" runat="server" />

    </form>
  </body>
</html>
<%@ 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">
' Called before a Delete operation.
    Private Sub NorthwindEmployeeDeleting(ByVal source As Object, ByVal e As ObjectDataSourceMethodEventArgs)

        ' The GridView passes the ID of the employee
        ' to be deleted. However, the business object, EmployeeLogic,
        ' requires a NorthwindEmployee parameter, named "ne". Create
        ' it now and add it to the parameters collection.
        Dim paramsFromPage As IDictionary = e.InputParameters
  
        If Not paramsFromPage("EmpID") Is Nothing Then
    
            Dim ne As New NorthwindEmployee(paramsFromPage("EmpID").ToString())
            ' Remove the old EmpID parameter.
            paramsFromPage.Clear()
            paramsFromPage.Add("ne", ne)
    
    
        End If
    End Sub ' NorthwindEmployeeDeleting

    ' Called after a Delete operation.
    Private Sub NorthwindEmployeeDeleted(ByVal source As Object, ByVal e As ObjectDataSourceStatusEventArgs)
        ' Handle the Exception if it is a NorthwindDataException.
        If Not e.Exception Is Nothing Then

            ' Handle the specific exception type. The ObjectDataSource wraps
            ' any Exceptions in a TargetInvokationException wrapper, so
            ' check the InnerException property for the expected Exception types.
            If e.Exception.InnerException.GetType().Equals(GetType(NorthwindDataException)) Then

                Label1.Text = e.Exception.InnerException.Message
                ' Because the exception is handled, there is
                ' no reason to throw it.
                e.ExceptionHandled = True
      
            End If
        End If
    End Sub ' NorthwindEmployeeDeleted
</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:gridview
          id="GridView1"
          runat="server"
          datasourceid="ObjectDataSource1"
          autogeneratedeletebutton="true"
          autogeneratecolumns="false"
          datakeynames="EmpID">
          <columns>
            <asp:boundfield headertext="EmpID" datafield="EmpID" />
            <asp:boundfield headertext="First Name" datafield="FirstName" />
            <asp:boundfield headertext="Last Name" datafield="LastName" />
          </columns>
        </asp:gridview>

        <asp:objectdatasource
          id="ObjectDataSource1"
          runat="server"
          selectmethod="GetAllEmployees"
          deletemethod="DeleteEmployee"
          ondeleting="NorthwindEmployeeDeleting"
          ondeleted="NorthwindEmployeeDeleted"
          typename="Samples.AspNet.VB.EmployeeLogic">
          <deleteparameters>
            <asp:parameter name="EmpID" type="Int32" />
          </deleteparameters>
        </asp:objectdatasource>

        <asp:label id="Label1" runat="server" />

    </form>
  </body>
</html>

注解

引发事件时,将通过委托调用事件处理程序。Raising an event invokes the event handler through a delegate. 有关如何处理事件的详细信息, 请参阅处理和引发事件For more information about how to handle events, see Handling and Raising Events.

OnDeleting 方法还允许派生类对事件进行处理而不必附加委托。The OnDeleting method also allows derived classes to handle the event without attaching a delegate. 这是在派生类中处理事件的首选技术。This is the preferred technique for handling the event in a derived class.

继承者说明

当在派生OnDeleting(ObjectDataSourceMethodEventArgs)类中重写方法时, 请确保OnDeleting(ObjectDataSourceMethodEventArgs)调用基类的方法, 以便注册的委托接收事件。When overriding the OnDeleting(ObjectDataSourceMethodEventArgs) method in a derived class, be sure to call the OnDeleting(ObjectDataSourceMethodEventArgs) method for the base class so that registered delegates receive the event.

适用于

另请参阅