ObjectDataSourceView.DeleteMethod Propriété

Définition

Obtient ou définit le nom de la méthode ou de la fonction que l'objet ObjectDataSourceView appelle pour supprimer des données.

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

Valeur de propriété

Chaîne qui représente le nom de la méthode ou de la fonction que ObjectDataSourceView utilise pour supprimer des données. La valeur par défaut est une chaîne vide ("").

Exemples

L’exemple de code suivant montre comment utiliser un ObjectDataSource contrôle avec un objet métier et un GridView contrôle pour supprimer des données. Le GridView affiche initialement un ensemble de tous les employés, à l’aide de la méthode spécifiée par la SelectMethod propriété pour récupérer les données de l’objet EmployeeLogic . Étant donné que la AutoGenerateDeleteButton propriété est définie sur true, le GridView contrôle affiche automatiquement un bouton Supprimer .

Si vous cliquez sur le bouton Supprimer , l’opération est effectuée à l’aide Delete de la méthode spécifiée par la DeleteMethod propriété et des paramètres spécifiés dans la DeleteParameters collection. Dans cet exemple de code, certaines étapes de prétraitement et de post-traitement sont également effectuées. Le NorthwindEmployeeDeleting délégué est appelé pour gérer l’événement Deleting avant l’exécution de l’opération Delete , et le NorthwindEmployeeDeleted délégué est appelé pour gérer l’événement une fois l’opération DeletedDelete terminée, pour effectuer une gestion des exceptions. Dans cet exemple, si un NorthwindDataException est levée, il est géré par ce délégué.

Pour examiner l’implémentation de l’objet EmployeeLogic métier de niveau intermédiaire utilisé par cet exemple de code, consultez 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>

Remarques

La méthode identifiée par la DeleteMethod propriété peut être une méthode instance ou une static méthode (Shared en Visual Basic). S’il s’agit d’une méthode instance, l’objet métier est créé et détruit chaque fois que la méthode spécifiée par la DeleteMethod propriété est appelée. Vous pouvez gérer l’événement ObjectCreated pour qu’il fonctionne avec l’objet métier avant que la méthode spécifiée par la DeleteMethod propriété soit appelée. Vous pouvez également gérer l’événement ObjectDisposing déclenché après l’appel de la méthode spécifiée par la DeleteMethod propriété . Si la méthode est une static méthode (Shared en Visual Basic), l’objet métier n’est jamais créé et vous ne pouvez pas gérer ces événements.

Si l’objet métier avec lequel le ObjectDataSource contrôle fonctionne implémente plusieurs méthodes ou fonctions portant le même nom (surcharges de méthode), le contrôle de source de données tente d’appeler la bonne en fonction d’un ensemble de conditions, y compris les paramètres de la DeleteParameters collection. Si les paramètres de la DeleteParameters collection ne correspondent pas à ceux de la signature de méthode DeleteMethod , la source de données lève une exception.

La valeur de la DeleteMethod propriété est stockée dans l’état d’affichage.

Pour plus d'informations, consultez DeleteMethod.

S’applique à

Voir aussi