ListViewDeletedEventArgs 類別

定義

提供 ItemDeleted 事件的資料。Provides data for the ItemDeleted event.

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

範例

下列範例顯示如何使用 ListViewDeletedEventArgs 物件來判斷刪除作業期間是否發生例外狀況。The following example shows how to use the ListViewDeletedEventArgs object to determine whether an exception occurred during a delete operation. ListViewDeletedEventArgs物件會傳遞至事件的事件處理方法 ItemDeletedThe ListViewDeletedEventArgs object is passed to the event-handling method for the ItemDeleted event.

<%@ 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="|&lt;&lt; " LastPageText=" &gt;&gt;|"
                NextPageText=" &gt; " PreviousPageText=" &lt; " />
            </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="|&lt;&lt; " LastPageText=" &gt;&gt;|"
                NextPageText=" &gt; " PreviousPageText=" &lt; " />
            </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>

備註

ListView ItemDeleted 當按一下控制項中的 [刪除] 按鈕或 DeleteItem 呼叫方法,但在控制項刪除專案之後,控制項就會引發事件 ListViewThe ListView control raises the ItemDeleted event when a Delete button in the control is clicked or the DeleteItem method is called, but after the ListView control deletes the item. ([刪除] 按鈕是其 CommandName 屬性設為 "Delete" 的按鈕。 ) 這可讓您提供事件處理方法,以便在每次發生此事件時執行自訂常式,例如檢查刪除作業的結果。(A Delete button is a button whose CommandName property is set to "Delete".) This enables you to provide an event-handling method that performs a custom routine whenever this event occurs, such as checking the results of a delete operation.

ListViewDeletedEventArgs物件會傳遞至事件處理方法。A ListViewDeletedEventArgs object is passed to the event-handling method. 這個物件可讓您判斷受影響的專案數,以及可能發生的任何例外狀況。This object enables you to determine the number of items affected and any exceptions that might have occurred. 若要判斷受到刪除作業影響的專案數,請使用 AffectedRows 屬性。To determine the number of items that were affected by the delete operation, use the AffectedRows property. 若要判斷是否發生任何例外狀況,請使用 Exception 屬性。To determine whether any exceptions occurred, use the Exception property. 您可以藉由設定屬性,來指出您是否已在事件處理方法中處理例外狀況 ExceptionHandledYou can indicate whether you have handled the exception in the event-handling method by setting the ExceptionHandled property.

注意

如果在刪除作業期間發生例外狀況,且 ExceptionHandled 屬性設定為 false ,則 ListView 控制項會重新擲回例外狀況。If an exception occurs during the delete operation and the ExceptionHandled property is set to false, the ListView control re-throws the exception.

若要存取已刪除專案的索引鍵欄位,請使用 Keys 屬性。To access the key fields of the deleted item, use the Keys property. 若要存取已刪除專案的非索引鍵欄位,請使用 Values 屬性。To access non-key fields of the deleted item, use the Values property. 如需 ListViewDeletedEventArgs 類別之執行個體的初始屬性值清單,請參閱 ListViewDeletedEventArgs 建構函式。For a list of initial property values for an instance of the ListViewDeletedEventArgs class, see the ListViewDeletedEventArgs constructor.

建構函式

ListViewDeletedEventArgs(Int32, Exception)

初始化 ListViewDeletedEventArgs 類別的新執行個體。Initializes a new instance of the ListViewDeletedEventArgs class.

屬性

AffectedRows

取得刪除作業所影響的資料列數目。Gets the number of rows that were affected by the delete operation.

Exception

取得刪除作業中引發的例外狀況 (如果有的話)。Gets the exception, if any, that was raised during the delete operation.

ExceptionHandled

取得或設定值,指出是否在事件處理常式中處理刪除作業中引發的例外狀況。Gets or sets a value that indicates whether an exception that was raised during the delete operation was handled in the event handler.

Keys

取得刪除之項目的索引鍵。Gets the key or keys for the deleted item.

Values

取得刪除之項目的非索引鍵欄位的值。Gets the non-key field values for the deleted item.

方法

Equals(Object)

判斷指定的物件是否等於目前的物件。Determines whether the specified object is equal to the current object.

(繼承來源 Object)
GetHashCode()

做為預設雜湊函式。Serves as the default hash function.

(繼承來源 Object)
GetType()

取得目前執行個體的 TypeGets the Type of the current instance.

(繼承來源 Object)
MemberwiseClone()

建立目前 Object 的淺層複製。Creates a shallow copy of the current Object.

(繼承來源 Object)
ToString()

傳回代表目前物件的字串。Returns a string that represents the current object.

(繼承來源 Object)

適用於

另請參閱