DetailsViewDeleteEventArgs Klasa

Definicja

Udostępnia dane dla zdarzenia ItemDeleting.

public ref class DetailsViewDeleteEventArgs : System::ComponentModel::CancelEventArgs
public class DetailsViewDeleteEventArgs : System.ComponentModel.CancelEventArgs
type DetailsViewDeleteEventArgs = class
    inherit CancelEventArgs
Public Class DetailsViewDeleteEventArgs
Inherits CancelEventArgs
Dziedziczenie
DetailsViewDeleteEventArgs

Przykłady

W poniższym przykładzie kodu pokazano, jak używać DetailsViewDeleteEventArgs obiektu przekazanego do procedury obsługi zdarzeń dla ItemDeleting zdarzenia w celu anulowania operacji usuwania.


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

Uwagi

Kontrolka DetailsView zgłasza ItemDeleting zdarzenie, gdy przycisk Usuń (przycisk z właściwością CommandName ustawioną na "Usuń") w kontrolce jest klikany, ale przed usunięciem rekordu DetailsView przez kontrolkę. Dzięki temu można udostępnić procedurę obsługi zdarzeń, która wykonuje niestandardową procedurę, taką jak weryfikowanie rekordu przed jego usunięciem, zawsze, gdy wystąpi to zdarzenie.

DetailsViewDeletedEventArgs Obiekt jest przekazywany do procedury obsługi zdarzeń, co pozwala określić indeks usuniętego rekordu i wskazać, że operacja usuwania powinna zostać anulowana. Aby określić indeks rekordu, użyj RowIndex właściwości . Aby anulować operację usuwania, ustaw Cancel właściwość na true. Możesz również uzyskać dostęp do pól kluczy i pól innych niż klucz, używając Keys odpowiednio właściwości i Values . Te wartości są przydatne, jeśli chcesz zweryfikować rekord przed jego usunięciem.

Uwaga

Istnieje możliwość zmodyfikowania wartości pól klucza we Keys właściwości. W przypadku zmiany tych wartości rekord odpowiadający nowym wartościom pola klucza zostanie usunięty.

Aby uzyskać więcej informacji na temat obsługi zdarzeń, zobacz Obsługa i podnoszenie zdarzeń.

Aby uzyskać listę początkowych DetailsViewDeleteEventArgs wartości właściwości dla wystąpienia klasy, zobacz DetailsViewDeleteEventArgs konstruktor.

Konstruktory

DetailsViewDeleteEventArgs(Int32)

Inicjuje nowe wystąpienie klasy DetailsViewDeleteEventArgs.

Właściwości

Cancel

Pobiera lub ustawia wartość wskazującą, czy zdarzenie powinno zostać anulowane.

(Odziedziczone po CancelEventArgs)
Keys

Pobiera uporządkowany słownik par nazwa/wartość pola klucza, który zawiera nazwy i wartości pól klucza usuniętych elementów.

RowIndex

Pobiera indeks usuniętego wiersza.

Values

Pobiera słownik par nazwa/wartość pola innego niż klucz, aby element został usunięty.

Metody

Equals(Object)

Określa, czy dany obiekt jest taki sam, jak bieżący obiekt.

(Odziedziczone po Object)
GetHashCode()

Służy jako domyślna funkcja skrótu.

(Odziedziczone po Object)
GetType()

Type Pobiera bieżące wystąpienie.

(Odziedziczone po Object)
MemberwiseClone()

Tworzy płytkią kopię bieżącego Objectelementu .

(Odziedziczone po Object)
ToString()

Zwraca ciąg reprezentujący bieżący obiekt.

(Odziedziczone po Object)

Dotyczy

Zobacz też