DetailsViewDeleteEventArgs DetailsViewDeleteEventArgs DetailsViewDeleteEventArgs DetailsViewDeleteEventArgs Class

Definizione

Fornisce dati per l'evento ItemDeleting.Provides data for the ItemDeleting event.

public ref class DetailsViewDeleteEventArgs : System::ComponentModel::CancelEventArgs
public class DetailsViewDeleteEventArgs : System.ComponentModel.CancelEventArgs
type DetailsViewDeleteEventArgs = class
    inherit CancelEventArgs
Public Class DetailsViewDeleteEventArgs
Inherits CancelEventArgs
Ereditarietà
DetailsViewDeleteEventArgsDetailsViewDeleteEventArgsDetailsViewDeleteEventArgsDetailsViewDeleteEventArgs

Esempi

Nell'esempio di codice riportato di seguito viene illustrato DetailsViewDeleteEventArgs come utilizzare l'oggetto passato al gestore eventi ItemDeleting per l'evento per annullare un'operazione di eliminazione.The following code example demonstrates how to use the DetailsViewDeleteEventArgs object passed to the event handler for the ItemDeleting event to cancel a delete operation.


<%@ 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>DetailsViewDeleteEventArgs Example</title>
</head>
<body>
    <form id="form1" runat="server">
        
      <h3>DetailsViewDeleteEventArgs Example</h3>
                
        <asp:detailsview id="CustomerDetailsView"
          datasourceid="DetailsViewSource"
          datakeynames="CustomerID"
          autogeneratedeletebutton="true"  
          autogeneraterows="true"
          allowpaging="true"
          onitemdeleting="CustomerDetailsView_ItemDeleting" 
          runat="server">
            
          <fieldheaderstyle backcolor="Navy"
            forecolor="White"/>
                    
        </asp:detailsview>
        
        <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 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>DetailsViewDeleteEventArgs Example</title>
</head>
<body>
    <form id="form1" runat="server">
        
      <h3>DetailsViewDeleteEventArgs Example</h3>
                
        <asp:detailsview id="CustomerDetailsView"
          datasourceid="DetailsViewSource"
          datakeynames="CustomerID"
          autogeneratedeletebutton="true"  
          autogeneraterows="true"
          allowpaging="true"
          onitemdeleting="CustomerDetailsView_ItemDeleting" 
          runat="server">
            
          <fieldheaderstyle backcolor="Navy"
            forecolor="White"/>
                    
        </asp:detailsview>
        
        <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>

Commenti

Il DetailsView controllo genera l' ItemDeleting evento quando viene fatto clic su un pulsante Elimina ( CommandName un pulsante con la relativa proprietà impostata su "Delete") all'interno del controllo, DetailsView ma prima che il controllo elimini il record.The DetailsView control raises the ItemDeleting event when a Delete button (a button with its CommandName property set to "Delete") within the control is clicked, but before the DetailsView control deletes the record. In questo modo è possibile fornire un gestore eventi che esegue una routine personalizzata, ad esempio la verifica di un record prima di eliminarlo, ogni volta che si verifica questo evento.This allows you to provide an event handler that performs a custom routine, such as verifying a record before deleting it, whenever this event occurs.

Un DetailsViewDeletedEventArgs oggetto viene passato al gestore eventi, che consente di determinare l'indice del record da eliminare e di indicare che l'operazione di eliminazione deve essere annullata.A DetailsViewDeletedEventArgs object is passed to the event handler, which allows you to determine the index of the record being deleted and to indicate that the delete operation should be canceled. Per determinare l'indice di un record, utilizzare la RowIndex proprietà.To determine the index of a record, use the RowIndex property. Per annullare l'operazione di eliminazione, impostare Cancel la proprietà truesu.To cancel the delete operation, set the Cancel property to true. È anche possibile accedere ai campi chiave e ai campi non chiave usando rispettivamente le Keys proprietà Values e.You can also access the key fields and non-key fields by using the Keys and Values properties, respectively. Questi valori sono utili se si desidera verificare il record prima di eliminarlo.These values are useful if you want to verify the record before deleting it.

Nota

È possibile modificare i valori dei campi chiave nella Keys proprietà.It is possible to modify the key field values in the Keys property. Se si modificano questi valori, il record che corrisponde ai nuovi valori dei campi chiave verrà eliminato.If you change these values, the record that corresponds to the new key field values will be deleted.

Per altre informazioni su come gestire gli eventi, vedere la gestione e generazione di eventi.For more information about how to handle events, see Handling and Raising Events.

Per un elenco dei valori iniziali delle proprietà di un'istanza DetailsViewDeleteEventArgs della classe, vedere DetailsViewDeleteEventArgs il costruttore.For a list of initial property values for an instance of DetailsViewDeleteEventArgs class, see the DetailsViewDeleteEventArgs constructor.

Costruttori

DetailsViewDeleteEventArgs(Int32) DetailsViewDeleteEventArgs(Int32) DetailsViewDeleteEventArgs(Int32) DetailsViewDeleteEventArgs(Int32)

Inizializza una nuova istanza della classe DetailsViewDeleteEventArgs.Initializes a new instance of the DetailsViewDeleteEventArgs class.

Proprietà

Cancel Cancel Cancel Cancel

Ottiene o imposta un valore che indica se l'evento debba essere annullato.Gets or sets a value indicating whether the event should be canceled.

(Inherited from CancelEventArgs)
Keys Keys Keys Keys

Ottiene un dizionario ordinato delle coppie nome/valore dei campi chiave che contiene i nomi e i valori dei campi chiave degli elementi eliminati.Gets an ordered dictionary of key field name/value pairs that contains the names and values of the key fields of the deleted items.

RowIndex RowIndex RowIndex RowIndex

Ottiene l'indice della riga da eliminare.Gets the index of the row being deleted.

Values Values Values Values

Ottiene un dizionario delle coppie nome/valore dei campi non chiave dell'elemento da eliminare.Gets a dictionary of the non-key field name/value pairs for the item to delete.

Metodi

Equals(Object) Equals(Object) Equals(Object) Equals(Object)

Determina se l'oggetto specificato è uguale all'oggetto corrente.Determines whether the specified object is equal to the current object.

(Inherited from Object)
GetHashCode() GetHashCode() GetHashCode() GetHashCode()

Funge da funzione hash predefinita.Serves as the default hash function.

(Inherited from Object)
GetType() GetType() GetType() GetType()

Ottiene l'oggetto Type dell'istanza corrente.Gets the Type of the current instance.

(Inherited from Object)
MemberwiseClone() MemberwiseClone() MemberwiseClone() MemberwiseClone()

Crea una copia superficiale dell'oggetto Object corrente.Creates a shallow copy of the current Object.

(Inherited from Object)
ToString() ToString() ToString() ToString()

Restituisce una stringa che rappresenta l'oggetto corrente.Returns a string that represents the current object.

(Inherited from Object)

Si applica a

Vedi anche