共用方式為


DetailsViewDeleteEventArgs 類別

定義

提供 ItemDeleting 事件的資料。

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

範例

下列程式碼範例示範如何使用 DetailsViewDeleteEventArgs 傳遞至事件處理常式的物件,讓 ItemDeleting 事件取消刪除作業。


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

備註

DetailsView當控制項的 [刪除] 按鈕 (控制項 CommandName 的 屬性設定為 「Delete」) ,但在控制項刪除記錄之前 DetailsView ,控制項就會引發 ItemDeleting 事件。 這可讓您提供執行自訂常式的事件處理常式,例如在刪除記錄之前驗證記錄,每當發生此事件時。

DetailsViewDeletedEventArgs物件會傳遞至事件處理常式,可讓您判斷要刪除之記錄的索引,並指出應該取消刪除作業。 若要判斷記錄的索引,請使用 RowIndex 屬性。 若要取消刪除作業,請將 Cancel 屬性設定為 true 。 您也可以分別使用 KeysValues 屬性來存取索引鍵欄位和非索引鍵欄位。 如果您想要在刪除記錄之前先確認記錄,這些值會很有用。

注意

可以修改 屬性中的 Keys 索引鍵域值。 如果您變更這些值,將會刪除對應至新索引鍵域值的記錄。

如需如何處理事件的詳細資訊,請參閱 處理和引發事件

如需 類別實例 DetailsViewDeleteEventArgs 的初始屬性值清單,請參閱 建 DetailsViewDeleteEventArgs 構函式。

建構函式

DetailsViewDeleteEventArgs(Int32)

初始化 DetailsViewDeleteEventArgs 類別的新執行個體。

屬性

Cancel

取得或設定值,這個值表示是否應該取消事件。

(繼承來源 CancelEventArgs)
Keys

取得索引鍵欄位名稱/值組的排序字典,其中包含已刪除項目之索引鍵欄位的名稱和值。

RowIndex

取得要刪除之資料列的索引。

Values

取得要刪除項目中非索引鍵欄位名稱/值組的字典。

方法

Equals(Object)

判斷指定的物件是否等於目前的物件。

(繼承來源 Object)
GetHashCode()

做為預設雜湊函式。

(繼承來源 Object)
GetType()

取得目前執行個體的 Type

(繼承來源 Object)
MemberwiseClone()

建立目前 Object 的淺層複製。

(繼承來源 Object)
ToString()

傳回代表目前物件的字串。

(繼承來源 Object)

適用於

另請參閱