ListViewDeletedEventArgs Klasa

Definicja

Udostępnia dane dla zdarzenia ItemDeleted.

public ref class ListViewDeletedEventArgs : EventArgs
public class ListViewDeletedEventArgs : EventArgs
type ListViewDeletedEventArgs = class
    inherit EventArgs
Public Class ListViewDeletedEventArgs
Inherits EventArgs
Dziedziczenie
ListViewDeletedEventArgs

Przykłady

W poniższym przykładzie pokazano, jak za pomocą ListViewDeletedEventArgs obiektu określić, czy wystąpił wyjątek podczas operacji usuwania. Obiekt ListViewDeletedEventArgs jest przekazywany do metody obsługi zdarzeń dla ItemDeleted zdarzenia.

<%@ 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()
    {
        Message.Text = String.Empty;
    }
    
    void ContactsListView_ItemDeleted(Object sender, ListViewDeletedEventArgs e)
    {
        // Determine whether an exception occurred during the delete operation.
        if (e.Exception == null)
        {
            // Ensure that a record was deleted.
            if (e.AffectedRows > 0)
            {
                Message.Text = e.AffectedRows + " item(s) deleted successfully.";
            }
            else
            {
                Message.Text = "No item was deleted.";
            }
        }
        else
        {
            // Insert the code to handle the exception here.

            // Indicate that the exception has been handled.
            e.ExceptionHandled = true;
            Message.Text = "An error occurred during the delete operation.";
        }
    }
    
</script>

<html xmlns="http://www.w3.org/1999/xhtml" >
  <head runat="server">
    <title>ListViewDeletedEventArgs Example</title>
  </head>
  <body>
    <form id="form1" runat="server">
        
      <h3>ListViewDeletedEventArgs Example</h3>
            
      <asp:Label ID="Message"
        ForeColor="Red"          
        runat="server"/>
      <br/>
            
      <asp:ListView ID="ContactsListView" 
        DataSourceID="ContactsDataSource" 
        DataKeyNames="ContactID"
        OnItemDeleted="ContactsListView_ItemDeleted"  
        runat="server">
        <LayoutTemplate>
          <table cellpadding="2" runat="server" id="tblContacts" width="640px">
            <tr runat="server" id="itemPlaceholder" />
          </table>
          <asp:DataPager runat="server" ID="PeopleDataPager" PageSize="12">
            <Fields>
              <asp:NextPreviousPagerField 
                ShowFirstPageButton="true" ShowLastPageButton="true"
                FirstPageText="|<< " LastPageText=" >>|"
                NextPageText=" > " PreviousPageText=" < " />
            </Fields>
          </asp:DataPager>
        </LayoutTemplate>
        <ItemTemplate>
          <tr runat="server">
            <td valign="top">
              <asp:Label ID="FirstNameLabel" runat="server" Text='<%#Eval("FirstName") %>' />
              <asp:Label ID="LastNameLabel" runat="server" Text='<%#Eval("LastName") %>' />
            </td>
            <td>
              <asp:Label ID="EmailLabel" runat="server" Text='<%#Eval("EmailAddress") %>' />
            </td>
            <td>
              <asp:LinkButton ID="DeleteButton" runat="server" 
                CommandName="Delete" 
                Text="Delete" 
                OnClientClick="return confirm('Are you sure?');" />
            </td>
          </tr>
        </ItemTemplate>
      </asp:ListView>

      <!-- This example uses Microsoft SQL Server and connects    -->
      <!-- to the AdventureWorks sample database. Use an ASP.NET  -->
      <!-- expression to retrieve the connection string value     -->
      <!-- from the Web.config file.                              -->            
      <asp:SqlDataSource ID="ContactsDataSource" runat="server" 
        ConnectionString="<%$ ConnectionStrings:AdventureWorks_DataConnectionString %>"
        SelectCommand="SELECT [ContactID], [FirstName], [LastName], [EmailAddress] 
          FROM Person.Contact"
        DeleteCommand="DELETE FROM Person.Contact WHERE [ContactID] = @ContactID">
        <DeleteParameters>
            <asp:Parameter Name="ContactID" Type="Int32" />
        </DeleteParameters>
      </asp:SqlDataSource>
      
    </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()
        Message.Text = String.Empty
    End Sub
    
    Sub ContactsListView_ItemDeleted(sender As Object, e As ListViewDeletedEventArgs)

        ' Determine whether an exception occurred during the delete operation.
        If e.Exception Is Nothing Then
            ' Ensure that a record was deleted.
            If e.AffectedRows > 0 Then
                Message.Text = e.AffectedRows.ToString() & _
                    " item(s) deleted successfully."
            Else
                Message.Text = "No item was deleted."
            End If
        Else
            ' Insert the code to handle the exception here.

            ' Indicate that the exception has been handled.
            e.ExceptionHandled = true
            Message.Text = "An error occurred during the delete operation."
        End If
    End Sub
    
</script>

<html xmlns="http://www.w3.org/1999/xhtml" >
  <head id="Head1" runat="server">
    <title>ListViewDeletedEventArgs Example</title>
  </head>
  <body>
    <form id="form1" runat="server">
        
      <h3>ListViewDeletedEventArgs Example</h3>
            
      <asp:Label ID="Message"
        ForeColor="Red"          
        runat="server"/>
      <br/>
            
      <asp:ListView ID="ContactsListView" 
        DataSourceID="ContactsDataSource" 
        DataKeyNames="ContactID"
        OnItemDeleted="ContactsListView_ItemDeleted"  
        runat="server">
        <LayoutTemplate>
          <table cellpadding="2" runat="server" id="tblContacts" width="640px">
            <tr runat="server" id="itemPlaceholder" />
          </table>
          <asp:DataPager runat="server" ID="PeopleDataPager" PageSize="12">
            <Fields>
              <asp:NextPreviousPagerField 
                ShowFirstPageButton="true" ShowLastPageButton="true"
                FirstPageText="|<< " LastPageText=" >>|"
                NextPageText=" > " PreviousPageText=" < " />
            </Fields>
          </asp:DataPager>
        </LayoutTemplate>
        <ItemTemplate>
          <tr runat="server">
            <td valign="top">
              <asp:Label ID="FirstNameLabel" runat="server" Text='<%#Eval("FirstName") %>' />
              <asp:Label ID="LastNameLabel" runat="server" Text='<%#Eval("LastName") %>' />
            </td>
            <td>
              <asp:Label ID="EmailLabel" runat="server" Text='<%#Eval("EmailAddress") %>' />
            </td>
            <td>
              <asp:LinkButton ID="DeleteButton" runat="server" 
                CommandName="Delete" 
                Text="Delete" 
                OnClientClick="return confirm('Are you sure?');" />
            </td>
          </tr>
        </ItemTemplate>
      </asp:ListView>

      <!-- This example uses Microsoft SQL Server and connects    -->
      <!-- to the AdventureWorks sample database. Use an ASP.NET  -->
      <!-- expression to retrieve the connection string value     -->
      <!-- from the Web.config file.                              -->            
      <asp:SqlDataSource ID="ContactsDataSource" runat="server" 
        ConnectionString="<%$ ConnectionStrings:AdventureWorks_DataConnectionString %>"
        SelectCommand="SELECT [ContactID], [FirstName], [LastName], [EmailAddress] 
          FROM Person.Contact"
        DeleteCommand="DELETE FROM Person.Contact WHERE [ContactID] = @ContactID">
        <DeleteParameters>
            <asp:Parameter Name="ContactID" Type="Int32" />
        </DeleteParameters>
      </asp:SqlDataSource>
      
    </form>
  </body>
</html>

Uwagi

Kontrolka ListView zgłasza ItemDeleted zdarzenie po kliknięciu przycisku Usuń w kontrolce lub DeleteItem wywołaniu metody, ale po ListView usunięciu elementu przez kontrolkę. (Przycisk Usuń to przycisk, którego CommandName właściwość jest ustawiona na "Usuń". Dzięki temu można podać metodę obsługi zdarzeń, która wykonuje niestandardową procedurę za każdym razem, gdy wystąpi to zdarzenie, na przykład sprawdzając wyniki operacji usuwania.

ListViewDeletedEventArgs Obiekt jest przekazywany do metody obsługi zdarzeń. Ten obiekt umożliwia określenie liczby elementów, których dotyczy problem, oraz wszelkich wyjątków, które mogły wystąpić. Aby określić liczbę elementów, których dotyczy operacja usuwania, użyj AffectedRows właściwości . Aby określić, czy wystąpiły jakiekolwiek wyjątki, użyj Exception właściwości . Możesz wskazać, czy obsłużono wyjątek w metodzie obsługi zdarzeń, ustawiając ExceptionHandled właściwość .

Uwaga

Jeśli podczas operacji usuwania wystąpi wyjątek, a ExceptionHandled właściwość jest ustawiona na false, ListView kontrolka ponownie zgłasza wyjątek.

Aby uzyskać dostęp do pól klucza usuniętego elementu, użyj Keys właściwości . Aby uzyskać dostęp do pól innych niż klucz usuniętego elementu, użyj Values właściwości . Aby uzyskać listę początkowych wartości właściwości dla wystąpienia ListViewDeletedEventArgs klasy, zobacz ListViewDeletedEventArgs konstruktor.

Konstruktory

ListViewDeletedEventArgs(Int32, Exception)

Inicjuje nowe wystąpienie klasy ListViewDeletedEventArgs.

Właściwości

AffectedRows

Pobiera liczbę wierszy, których dotyczy operacja usuwania.

Exception

Pobiera wyjątek, jeśli istnieje, który został zgłoszony podczas operacji usuwania.

ExceptionHandled

Pobiera lub ustawia wartość wskazującą, czy wyjątek, który został zgłoszony podczas operacji usuwania, został obsłużony w procedurze obsługi zdarzeń.

Keys

Pobiera klucz lub klucze dla usuniętego elementu.

Values

Pobiera wartości pól innych niż klucz dla usuniętego elementu.

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 wartość bieżącego wystąpienia.

(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ż