ObjectDataSourceView.DeleteMethod Proprietà

Definizione

Ottiene o imposta il nome del metodo o funzione che l'oggetto ObjectDataSourceView richiama per eliminare i dati.Gets or sets the name of the method or function that the ObjectDataSourceView object invokes to delete data.

public:
 property System::String ^ DeleteMethod { System::String ^ get(); void set(System::String ^ value); };
public string DeleteMethod { get; set; }
member this.DeleteMethod : string with get, set
Public Property DeleteMethod As String

Valore della proprietà

Stringa che rappresenta il nome del metodo o funzione che l'oggetto ObjectDataSourceView utilizza per eliminare i dati.A string that represents the name of the method or function that the ObjectDataSourceView uses to delete data. Il valore predefinito è una stringa vuota ("").The default is an empty string ("").

Esempi

Nell'esempio di codice riportato di seguito viene illustrato ObjectDataSource come utilizzare un controllo con un oggetto GridView business e un controllo per eliminare i dati.The following code example demonstrates how to use an ObjectDataSource control with a business object and a GridView control to delete data. Inizialmente viene visualizzato un set di tutti i dipendenti, utilizzando il metodo specificato SelectMethod dalla proprietà per EmployeeLogic recuperare i dati dall'oggetto. 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. Poiché la AutoGenerateDeleteButton proprietà è impostata su true, il GridView controllo Visualizza automaticamente un pulsante Elimina .Because the AutoGenerateDeleteButton property is set to true, the GridView control automatically displays a Delete button.

Se si fa clic sul pulsante Elimina , Delete l'operazione viene eseguita utilizzando il metodo specificato dalla DeleteMethod proprietà e DeleteParameters tutti i parametri specificati nella raccolta.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 questo esempio di codice vengono eseguiti anche alcuni passaggi di pre-elaborazione e post-elaborazione.In this code example, some preprocessing and post-processing steps are also performed. Il NorthwindEmployeeDeleting delegato viene chiamato per gestire l' Deleting evento prima dell' Delete esecuzione dell'operazione e il NorthwindEmployeeDeleted delegato viene chiamato per gestire l' Deleted evento dopo il completamento Delete dell'operazione, fino a eseguire una gestione delle eccezioni.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. In questo esempio, se viene NorthwindDataException generata un'eccezione, questa viene gestita da questo delegato.In this example, if a NorthwindDataException is thrown, it is handled by this delegate.

Per esaminare l'implementazione dell'oggetto EmployeeLogic business di livello intermedio utilizzato in questo esempio di codice, ObjectDataSourceStatusEventArgsvedere.To 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>

Commenti

Il metodo identificato dalla DeleteMethod proprietà può essere un metodo di istanza o un static metodo (Shared in Visual Basic).The method that is identified by the DeleteMethod property can be an instance method or a static (Shared in Visual Basic) method. Se si tratta di un metodo di istanza, l'oggetto business viene creato ed eliminato ogni volta che viene chiamato DeleteMethod il metodo specificato dalla proprietà.If it is an instance method, the business object is created and destroyed each time the method specified by the DeleteMethod property is called. È possibile gestire l' ObjectCreated evento in modo che funzioni con l'oggetto business prima che venga chiamato DeleteMethod il metodo specificato dalla proprietà.You can handle the ObjectCreated event to work with the business object before the method specified by the DeleteMethod property is called. È anche possibile gestire l' ObjectDisposing evento generato dopo la chiamata del metodo specificato DeleteMethod dalla proprietà.You can also handle the ObjectDisposing event that is raised after the method specified by the DeleteMethod property is called. Se il metodo è un static metodoShared (in Visual Basic), l'oggetto business non viene mai creato e non è possibile gestire questi eventi.If the method is a static (Shared in Visual Basic) method, the business object is never created and you cannot handle these events.

Se l'oggetto business con cui ObjectDataSource funziona il controllo implementa più di un metodo o funzione con lo stesso nome (overload del metodo), il controllo origine dati tenta di richiamare quello corretto in base a un set di condizioni, inclusi i parametri. DeleteParameters nella raccolta.If the business object that the ObjectDataSource control works with implements more than one method or function with the same name (method overloads), the data source control attempts to invoke the correct one according to a set of conditions, including the parameters in the DeleteParameters collection. Se i parametri nella DeleteParameters raccolta non corrispondono a quelli DeleteMethod della firma del metodo, l'origine dati genera un'eccezione.If the parameters in the DeleteParameters collection do not match those of the DeleteMethod method signature, the data source throws an exception.

Il valore della DeleteMethod proprietà viene archiviato nello stato di visualizzazione.The value of the DeleteMethod property is stored in view state.

Per ulteriori informazioni, vedere DeleteMethod.For more information, see DeleteMethod.

Si applica a

Vedi anche