FormViewDeletedEventArgs Classe

Définition

Fournit des données pour l'événement ItemDeleted.

public ref class FormViewDeletedEventArgs : EventArgs
public class FormViewDeletedEventArgs : EventArgs
type FormViewDeletedEventArgs = class
    inherit EventArgs
Public Class FormViewDeletedEventArgs
Inherits EventArgs
Héritage
FormViewDeletedEventArgs

Exemples

L’exemple suivant montre comment utiliser l’objet FormViewDeletedEventArgs passé à la méthode de gestion des événements pour l’événement ItemDeleted afin de déterminer si une exception s’est produite pendant une opération de suppression.


<%@ 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 EmployeeFormView_ItemDeleted(Object sender, FormViewDeletedEventArgs e)
  {
    // Use the Exception property to determine whether an exception
    // occurred during the delete operation.
    if (e.Exception == null)
    {
      // Use the AffectedRows property to determine whether the
      // record was deleted. Sometimes an error might occur that 
      // does not raise an exception, but prevents the delete
      // operation from completing.
      if (e.AffectedRows == 1)
      {
        MessageLabel.Text = "Record deleted successfully.";
      }
      else
      {
        MessageLabel.Text = "An error occurred during the delete operation.";
      }
    }
    else
    {
      // Insert the code to handle the exception.
      MessageLabel.Text = e.Exception.Message;
      
      // Use the ExceptionHandled property to indicate that the 
      // exception has already been handled.
      e.ExceptionHandled = true;
    }
  }

</script>

<html xmlns="http://www.w3.org/1999/xhtml" >
  <head runat="server">
    <title>FormViewDeletedEventArgs Example</title>
</head>
<body>
    <form id="form1" runat="server">
        
      <h3>FormViewDeletedEventArgs Example</h3>
                       
      <asp:formview id="EmployeeFormView"
        datasourceid="EmployeeSource"
        allowpaging="true"
        datakeynames="EmployeeID"
        onitemdeleted="EmployeeFormView_ItemDeleted"  
        runat="server">
        
        <itemtemplate>
        
          <table>
            <tr>
              <td>
                <asp:image id="EmployeeImage"
                  imageurl='<%# Eval("PhotoPath") %>'
                  alternatetext='<%# Eval("LastName") %>' 
                  runat="server"/>
              </td>
              <td>
                <h3><%# Eval("FirstName") %> <%# Eval("LastName") %></h3>      
                <%# Eval("Title") %>        
              </td>
            </tr>
            <tr>
              <td colspan="2">
                <asp:button id="DeleteButton"
                  text="Delete Record"
                  commandname="Delete"
                  runat="server" />
              </td>
            </tr>
          </table>
        
        </itemtemplate>         
                  
      </asp:formview>
      
      <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="EmployeeSource"
        selectcommand="Select [EmployeeID], [LastName], [FirstName], [Title], [PhotoPath] From [Employees]"
        deletecommand="Delete [Employees] Where [EmployeeID]=@EmployeeID"
        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 EmployeeFormView_ItemDeleted(ByVal sender As Object, ByVal e As FormViewDeletedEventArgs) Handles EmployeeFormView.ItemDeleted
  
    ' Use the Exception property to determine whether an exception
    ' occurred during the delete operation.
    If e.Exception Is Nothing Then
    
      ' Use the AffectedRows property to determine whether the
      ' record was deleted. Sometimes an error might occur that 
      ' does not raise an exception, but prevents the delete
      ' operation from completing.
      If e.AffectedRows = 1 Then
      
        MessageLabel.Text = "Record deleted successfully."
      
      Else
              
        MessageLabel.Text = "An error occurred during the delete operation."
      
      End If
    
    Else
    
      ' Insert the code to handle the exception.
      MessageLabel.Text = e.Exception.Message
      
      ' Use the ExceptionHandled property to indicate that the 
      ' exception has already been handled.
      e.ExceptionHandled = True
      
    End If
    
  End Sub

</script>

<html xmlns="http://www.w3.org/1999/xhtml" >
  <head runat="server">
    <title>FormViewDeletedEventArgs Example</title>
</head>
<body>
    <form id="form1" runat="server">
        
      <h3>FormViewDeletedEventArgs Example</h3>
                       
      <asp:formview id="EmployeeFormView"
        datasourceid="EmployeeSource"
        allowpaging="true"
        datakeynames="EmployeeID"
        runat="server">
        
        <itemtemplate>
        
          <table>
            <tr>
              <td>
                <asp:image id="EmployeeImage"
                  imageurl='<%# Eval("PhotoPath") %>'
                  alternatetext='<%# Eval("LastName") %>' 
                  runat="server"/>
              </td>
              <td>
                <h3><%# Eval("FirstName") %> <%# Eval("LastName") %></h3>      
                <%# Eval("Title") %>        
              </td>
            </tr>
            <tr>
              <td colspan="2">
                <asp:button id="DeleteButton"
                  text="Delete Record"
                  commandname="Delete"
                  runat="server" />
              </td>
            </tr>
          </table>
        
        </itemtemplate>         
                  
      </asp:formview>
      
      <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="EmployeeSource"
        selectcommand="Select [EmployeeID], [LastName], [FirstName], [Title], [PhotoPath] From [Employees]"
        deletecommand="Delete [Employees] Where [EmployeeID]=@EmployeeID"
        connectionstring="<%$ ConnectionStrings:NorthWindConnectionString%>" 
        runat="server"/>
            
    </form>
  </body>
</html>

Remarques

Le FormView contrôle déclenche l’événement ItemDeleted 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 FormView contrôle a supprimé l’enregistrement. Cela vous permet de fournir une méthode de gestion des événements qui effectue une routine personnalisée, telle que la vérification des résultats d’une opération de suppression, chaque fois que cet événement se produit.

Un FormViewDeletedEventArgs objet est passé à la méthode de gestion des événements, ce qui vous permet de déterminer le nombre d’enregistrements affectés et toutes les exceptions qui ont pu se produire. Pour déterminer le nombre d’enregistrements affectés par l’opération de suppression, utilisez la AffectedRows propriété . Utilisez la Exception propriété pour déterminer si des exceptions se sont produites. Vous pouvez également indiquer si l’exception a été gérée dans la méthode de gestion des événements en définissant la ExceptionHandled propriété .

Notes

Si une exception se produit pendant l’opération de suppression et que la propriété a la ExceptionHandled valeur false, le FormView contrôle lève de nouveau l’exception.

Si vous souhaitez accéder aux paires nom/valeur des champs de clé et des champs non clés de l’enregistrement supprimé, utilisez les Keys propriétés et Values , respectivement.

Pour plus d’informations sur la façon de gérer les événements, consultez gestion et déclenchement d’événements.

Constructeurs

FormViewDeletedEventArgs(Int32, Exception)

Initialise une nouvelle instance de la classe FormViewDeletedEventArgs.

Propriétés

AffectedRows

Obtient le nombre de lignes affectées par l'opération de suppression.

Exception

Obtient l'exception (s'il y en a une) qui a été levée pendant l'opération de suppression.

ExceptionHandled

Obtient ou définit une valeur indiquant si une exception levée pendant l'opération de suppression a été gérée dans le gestionnaire d'événements.

Keys

Obtient un dictionnaire trié de paires nom/valeur de champ clé pour l'enregistrement supprimé.

Values

Obtient un dictionnaire des paires nom/valeur de champ ne correspondant pas à une clé pour l'enregistrement supprimé.

Méthodes

Equals(Object)

Détermine si l'objet spécifié est égal à l'objet actuel.

(Hérité de Object)
GetHashCode()

Fait office de fonction de hachage par défaut.

(Hérité de Object)
GetType()

Obtient le Type de l'instance actuelle.

(Hérité de Object)
MemberwiseClone()

Crée une copie superficielle du Object actuel.

(Hérité de Object)
ToString()

Retourne une chaîne qui représente l'objet actuel.

(Hérité de Object)

S’applique à

Voir aussi