GridView.RowDeleted Zdarzenie

Definicja

Występuje po kliknięciu przycisku Usuń wiersza, ale po usunięciu GridView wiersza.

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 

Typ zdarzenia

Przykłady

W poniższym przykładzie pokazano, jak za pomocą RowDeleted zdarzenia sprawdzić wynik operacji usuwania. Zostanie wyświetlony komunikat wskazujący użytkownikowi, czy operacja zakończyła się pomyślnie.


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

Uwagi

Zdarzenie RowDeleted jest wywoływane po kliknięciu przycisku Usuń wiersza, ale po usunięciu GridView wiersza przez kontrolkę. Dzięki temu można podać metodę obsługi zdarzeń, która wykonuje niestandardową procedurę, taką jak sprawdzanie wyników operacji usuwania, za każdym razem, gdy wystąpi to zdarzenie.

GridViewDeletedEventArgs Obiekt jest przekazywany do metody obsługi zdarzeń, która umożliwia określenie liczby wierszy, których dotyczy problem, oraz wszelkich wyjątków, które mogły wystąpić. Można również wskazać, czy wyjątek został obsłużony w metodzie obsługi zdarzeń, ustawiając ExceptionHandled właściwość GridViewDeletedEventArgs obiektu.

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

Dotyczy

Zobacz też