共用方式為


GridView.RowDeleted 事件

定義

按一下資料列的 [刪除] 按鈕時發生 (但在 GridView 控制項刪除資料列之後)。

public:
 event System::Web::UI::WebControls::GridViewDeletedEventHandler ^ RowDeleted;
public event System.Web.UI.WebControls.GridViewDeletedEventHandler RowDeleted;
member this.RowDeleted : System.Web.UI.WebControls.GridViewDeletedEventHandler 
Public Custom Event RowDeleted As GridViewDeletedEventHandler 

事件類型

範例

下列範例示範如何使用 RowDeleted 事件來檢查刪除作業的結果。 隨即顯示訊息,向使用者指出作業是否成功。


<%@ 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 CustomersGridView_RowDeleted(Object sender, GridViewDeletedEventArgs e)
  {
    
    // Display whether the delete operation succeeded.
    if(e.Exception == null)
    {
      Message.Text = "Row deleted successfully.";
    }
    else
    {
      Message.Text = "An error occurred while attempting to delete the row.";
      e.ExceptionHandled = true;   
    }
    
  }
    
</script>

<html xmlns="http://www.w3.org/1999/xhtml" >
  <head runat="server">
    <title>GridView RowDeleted Example</title>
</head>
<body>
    <form id="form1" runat="server">
        
      <h3>GridView RowDeleted Example</h3>
            
      <asp:label id="Message"
        forecolor="Red"          
        runat="server"/>
                
      <br/>
            
      <asp:gridview id="CustomersGridView" 
        datasourceid="CustomersSqlDataSource" 
        autogeneratecolumns="true"
        autogeneratedeletebutton="true"
        datakeynames="CustomerID"
        onrowdeleted="CustomersGridView_RowDeleted"  
        runat="server">
      </asp:gridview>
            
      <!-- 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="CustomersSqlDataSource"  
        selectcommand="Select [CustomerID], [CompanyName], [Address], [City], [PostalCode], [Country] From [Customers]"
        deletecommand="Delete from Customers where CustomerID = @CustomerID"
        connectionstring="<%$ ConnectionStrings:NorthWindConnectionString%>"
        runat="server">
      </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 CustomersGridView_RowDeleted(sender As Object, e As GridViewDeletedEventArgs)
    
    ' Display whether the delete operation succeeded.
    If e.Exception Is Nothing Then
    
      Message.Text = "Row deleted successfully."
    
    Else
          
      Message.Text = "An error occurred while attempting to delete the row."
      e.ExceptionHandled = True
      
    End If
    
  End Sub
    
</script>

<html xmlns="http://www.w3.org/1999/xhtml" >
  <head runat="server">
    <title>GridView RowDeleted Example</title>
</head>
<body>
    <form id="form1" runat="server">
        
      <h3>GridView RowDeleted Example</h3>
            
      <asp:label id="Message"
        forecolor="Red"          
        runat="server"/>
                
      <br/>

      <asp:gridview id="CustomersGridView" 
        datasourceid="CustomersSqlDataSource" 
        autogeneratecolumns="true"
        autogeneratedeletebutton="true"
        datakeynames="CustomerID"
        onrowdeleted="CustomersGridView_RowDeleted"  
        runat="server">
      </asp:gridview>
            
      <!-- 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="CustomersSqlDataSource"  
        selectcommand="Select [CustomerID], [CompanyName], [Address], [City], [PostalCode], [Country] From [Customers]"
        deletecommand="Delete from Customers where CustomerID = @CustomerID"
        connectionstring="<%$ ConnectionStrings:NorthWindConnectionString%>"
        runat="server">
      </asp:sqldatasource>
      
    </form>
  </body>
</html>

備註

RowDeleted按一下資料列的 [刪除] 按鈕時,就會引發 事件,但在控制項刪除資料列之後 GridView 。 這可讓您提供執行自訂常式的事件處理方法,例如每當發生此事件時檢查刪除作業的結果。

GridViewDeletedEventArgs物件會傳遞至事件處理方法,可讓您判斷受影響的資料列數目,以及可能發生的任何例外狀況。 您也可以藉由設定 ExceptionHandled 物件的 屬性 GridViewDeletedEventArgs ,指出事件處理方法中是否已處理例外狀況。

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

適用於

另請參閱