DetailsViewDeleteEventHandler Délégué

Définition

Représente la méthode qui gère l'événement ItemDeleting d'un contrôle DetailsView.

public delegate void DetailsViewDeleteEventHandler(System::Object ^ sender, DetailsViewDeleteEventArgs ^ e);
public delegate void DetailsViewDeleteEventHandler(object sender, DetailsViewDeleteEventArgs e);
type DetailsViewDeleteEventHandler = delegate of obj * DetailsViewDeleteEventArgs -> unit
Public Delegate Sub DetailsViewDeleteEventHandler(sender As Object, e As DetailsViewDeleteEventArgs)

Paramètres

sender
Object

Source de l'événement.

e
DetailsViewDeleteEventArgs

DetailsViewDeleteEventArgs qui contient les données d’événement.

Exemples

L’exemple de code suivant montre comment ajouter par programmation un DetailsViewDeleteEventHandler délégué à l’événement ItemDeleting d’un DetailsView contrôle.


<%@ 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">

  void Page_Load(Object sender, EventArgs e)
  {
    
    // Create a new DetailsView object.
    DetailsView customerDetailsView = new DetailsView();
    
    // Set the DetailsView object's properties.
    customerDetailsView.ID = "CustomerDetailsView";
    customerDetailsView.DataSourceID = "DetailsViewSource";
    customerDetailsView.AutoGenerateRows = true;
    customerDetailsView.AutoGenerateDeleteButton = true;
    customerDetailsView.AllowPaging = true;
    customerDetailsView.PagerSettings.Mode = PagerButtons.NextPrevious;
    customerDetailsView.DataKeyNames = new String[1] { "CustomerID" };

    // Programmatically register the event-handling method
    // for the ItemDeleting event of a DetailsView control.
    customerDetailsView.ItemDeleting += new DetailsViewDeleteEventHandler(this.CustomerDetailsView_ItemDeleting);

    // Add the DetailsView object to the Controls collection
    // of the PlaceHolder control.
    DetailsViewPlaceHolder.Controls.Add(customerDetailsView);

  }

  void CustomerDetailsView_ItemDeleting(Object sender, DetailsViewDeleteEventArgs e)
  {

    // Get customer ID and name from the Keys and Values
    // properties.
    String keyValue = e.Keys["CustomerID"].ToString();
    String customerName = e.Values["CompanyName"].ToString();

    // Cancel the delete operation if the user attempts to 
    // delete protected record. In this example, records
    // with a customer ID that starts with with "A" cannot
    // be deleted.
    if (keyValue.StartsWith("A"))
    {
      e.Cancel = true;
      MessageLabel.Text = "You cannot delete " +
        customerName + ". This customer is protected.";
    }
    else
    {
      MessageLabel.Text = "Row " + e.RowIndex.ToString() +
        " deleted.";
    }

  }

</script>
 
 <html xmlns="http://www.w3.org/1999/xhtml" >   
  <head runat="server">
    <title>DetailsViewDeleteEventHandler Example</title>
</head>
<body>
    <form id="form1" runat="server">
        
      <h3>DetailsViewDeleteEventHandler Example</h3>
        
        <!-- Use a PlaceHolder control as the container for the -->
        <!-- dynamically generated DetailsView control.         -->       
        <asp:PlaceHolder id="DetailsViewPlaceHolder"
          runat="server"/>
          
        <br/><br/>
        
        <asp:label id="MessageLabel"
          forecolor="Red"
          runat="server"/>
            
        <!-- This example uses Microsoft SQL Server and connects  -->
        <!-- to the Northwind sample database. Use an ASP.NET     -->
        <!-- expression to retrieve the connection string value   -->
        <!-- from the web.config file.                            -->
        <asp:sqldatasource id="DetailsViewSource"
          selectcommand="Select [CustomerID], [CompanyName], [Address], 
            [City], [PostalCode], [Country] From [Customers]"
          deletecommand="Delete [Customers] 
            Where [CustomerID]=@CustomerID"
          connectionstring=
            "<%$ ConnectionStrings:NorthWindConnectionString%>" 
          runat="server"/>
            
      </form>
  </body>
</html>

<%@ 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">

  Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs)
    
    ' Create a new DetailsView object.
    Dim customerDetailsView As New DetailsView()
    
    ' Set the DetailsView object's properties.
    customerDetailsView.ID = "CustomerDetailsView"
    customerDetailsView.DataSourceID = "DetailsViewSource"
    customerDetailsView.AutoGenerateRows = True
    customerDetailsView.AutoGenerateDeleteButton = True
    customerDetailsView.AllowPaging = True
    customerDetailsView.PagerSettings.Mode = PagerButtons.NextPrevious
    
    Dim keyArray() As String = {"CustomerID"}
    customerDetailsView.DataKeyNames = keyArray
    
    ' Programmatically register the event-handling method
    ' for the ItemDeleting event of a DetailsView control.
    AddHandler customerDetailsView.ItemDeleting, AddressOf CustomerDetailsView_ItemDeleting

    ' Add the DetailsView object to the Controls collection
    ' of the PlaceHolder control.
    DetailsViewPlaceHolder.Controls.Add(customerDetailsView)

  End Sub

  Sub CustomerDetailsView_ItemDeleting(ByVal sender As Object, ByVal e As DetailsViewDeleteEventArgs)

    ' Get customer ID and name from the Keys and Values
    ' properties.
    Dim keyValue As String = e.Keys("CustomerID").ToString()
    Dim customerName As String = e.Values("CompanyName").ToString()

    ' Cancel the delete operation if the user attempts to 
    ' delete protected record. In this example, records
    ' with a customer ID that starts with with "A" cannot
    ' be deleted.
    If keyValue.StartsWith("A") Then

      e.Cancel = True
      MessageLabel.Text = "You cannot delete " & _
        customerName & ". This customer is protected."
  
    Else
  
      MessageLabel.Text = "Row " & e.RowIndex.ToString() & _
        " deleted."
  
    End If
  
  End Sub

</script>
  
<html xmlns="http://www.w3.org/1999/xhtml" >    
  <head runat="server">
    <title>DetailsViewDeleteEventHandler Example</title>
</head>
<body>
    <form id="form1" runat="server">
        
      <h3>DetailsViewDeleteEventHandler Example</h3>
        
        <!-- Use a PlaceHolder control as the container for the -->
        <!-- dynamically generated DetailsView control.         -->       
        <asp:PlaceHolder id="DetailsViewPlaceHolder"
          runat="server"/>
          
        <br/><br/>
        
        <asp:label id="MessageLabel"
          forecolor="Red"
          runat="server"/>
            
        <!-- This example uses Microsoft SQL Server and connects  -->
        <!-- to the Northwind sample database. Use an ASP.NET     -->
        <!-- expression to retrieve the connection string value   -->
        <!-- from the web.config file.                            -->
        <asp:sqldatasource id="DetailsViewSource"
          selectcommand="Select [CustomerID], [CompanyName], [Address], 
            [City], [PostalCode], [Country] From [Customers]"
          deletecommand="Delete [Customers] 
            Where [CustomerID]=@CustomerID"
          connectionstring=
            "<%$ ConnectionStrings:NorthWindConnectionString%>" 
          runat="server"/>
            
      </form>
  </body>
</html>

L’exemple de code suivant montre comment ajouter de manière déclarative un DetailsViewDeleteEventHandler délégué à l’événement ItemDeleting d’un DetailsView contrôle.


<%@ 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">

  void CustomerDetailsView_ItemDeleting(Object sender, 
    DetailsViewDeleteEventArgs e)
  {

    // Get customer ID and name from the Keys and Values
    // properties.
    String keyValue = e.Keys["CustomerID"].ToString();
    String customerName = e.Values["CompanyName"].ToString();

    // Cancel the delete operation if the user attempts to 
    // delete protected record. In this example, records
    // with a customer ID that starts with with "A" cannot
    // be deleted.
    if (keyValue.StartsWith("A"))
    {
      e.Cancel = true;
      MessageLabel.Text = "You cannot delete " +
        customerName + ". This customer is protected.";
    }
    else
    {
      MessageLabel.Text = "Row " + e.RowIndex.ToString() +
        " deleted.";
    }

  }

</script>
 
 <html xmlns="http://www.w3.org/1999/xhtml" >   
  <head runat="server">
    <title>DetailsViewDeleteEventHandler Example</title>
</head>
<body>
    <form id="form1" runat="server">
        
      <h3>DetailsViewDeleteEventHandler Example</h3>
        
        <asp:detailsview id="CustomerDetailsView"
          datasourceid="DetailsViewSource"
          autogeneraterows="true"
          autogeneratedeletebutton="true"
          allowpaging="true"
          datakeynames="CustomerID"
          onitemdeleting="CustomerDetailsView_ItemDeleting"  
          runat="server">
        
          <pagersettings mode="NextPrevious"/>
        
        </asp:detailsview>
          
        <br/><br/>
        
        <asp:label id="MessageLabel"
          forecolor="Red"
          runat="server"/>
            
        <!-- This example uses Microsoft SQL Server and connects  -->
        <!-- to the Northwind sample database. Use an ASP.NET     -->
        <!-- expression to retrieve the connection string value   -->
        <!-- from the web.config file.                            -->
        <asp:sqldatasource id="DetailsViewSource"
          selectcommand="Select [CustomerID], [CompanyName], [Address], 
            [City], [PostalCode], [Country] From [Customers]"
          deletecommand="Delete [Customers] 
            Where [CustomerID]=@CustomerID"
          connectionstring=
            "<%$ ConnectionStrings:NorthWindConnectionString%>" 
          runat="server"/>
            
      </form>
  </body>
</html>

<%@ page language="VB" autoeventwireup="false"%>
  
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<script runat="server">

  Sub CustomerDetailsView_ItemDeleting(ByVal sender As Object, _
    ByVal e As DetailsViewDeleteEventArgs) _
    Handles CustomerDetailsView.ItemDeleting

    ' Get customer ID and name from the Keys and Values
    ' properties.
    Dim keyValue As String = e.Keys("CustomerID").ToString()
    Dim customerName As String = e.Values("CompanyName").ToString()

    ' Cancel the delete operation if the user attempts to 
    ' delete protected record. In this example, records
    ' with a customer ID that starts with with "A" cannot
    ' be deleted.
    If keyValue.StartsWith("A") Then

      e.Cancel = True
      MessageLabel.Text = "You cannot delete " & _
        customerName & ". This customer is protected."
  
    Else
  
      MessageLabel.Text = "Row " & e.RowIndex.ToString() & _
        " deleted."
  
    End If
  
  End Sub

</script>
  
<html xmlns="http://www.w3.org/1999/xhtml" >    
  <head runat="server">
    <title>DetailsViewDeleteEventHandler Example</title>
</head>
<body>
    <form id="form1" runat="server">
        
      <h3>DetailsViewDeleteEventHandler Example</h3>
        
        <asp:detailsview id="CustomerDetailsView"
          datasourceid="DetailsViewSource"
          autogeneraterows="true"
          autogeneratedeletebutton="true"
          allowpaging="true"
          datakeynames="CustomerID" 
          runat="server">
        
          <pagersettings mode="NextPrevious"/>
        
        </asp:detailsview>
          
        <br/><br/>
        
        <asp:label id="MessageLabel"
          forecolor="Red"
          runat="server"/>
            
        <!-- This example uses Microsoft SQL Server and connects  -->
        <!-- to the Northwind sample database. Use an ASP.NET     -->
        <!-- expression to retrieve the connection string value   -->
        <!-- from the web.config file.                            -->
        <asp:sqldatasource id="DetailsViewSource"
          selectcommand="Select [CustomerID], [CompanyName], [Address], 
            [City], [PostalCode], [Country] From [Customers]"
          deletecommand="Delete [Customers] 
            Where [CustomerID]=@CustomerID"
          connectionstring=
            "<%$ ConnectionStrings:NorthWindConnectionString%>" 
          runat="server"/>
            
      </form>
  </body>
</html>

Remarques

Le DetailsView contrôle déclenche l’événement ItemDeleting lorsqu’un bouton Supprimer (un bouton dont CommandName la propriété est définie sur « Supprimer ») dans le contrôle est cliqué, mais après que le DetailsView contrôle a supprimé l’enregistrement. Cela vous permet de fournir un gestionnaire d’événements qui effectue une routine personnalisée, comme la vérification des résultats d’une opération de suppression, chaque fois que cet événement se produit.

Lorsque vous créez un délégué DetailsViewDeleteEventHandler, vous identifiez la méthode qui gérera l'événement. Pour associer l'événement au gestionnaire d'événements, ajoutez une instance du délégué à l'événement. Le gestionnaire d'événements est appelé chaque fois qu'un événement se produit, sauf si vous supprimez le délégué. Pour plus d’informations sur les délégués du gestionnaire d’événements, consultez Gestion et levée d’événements.

Méthodes d’extension

GetMethodInfo(Delegate)

Obtient un objet qui représente la méthode représentée par le délégué spécifié.

S’applique à

Voir aussi