GridView.RowDeleting 事件

定义

在单击某一行的“删除”按钮时,但在 GridView 控件删除该行之前发生。Occurs when a row's Delete button is clicked, but before the GridView control deletes the row.

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

示例

下面的示例演示如何使用RowDeleting事件来取消删除操作。The following example demonstrates how to use the RowDeleting event to cancel the delete operation. 页面包含一个GridView控件, 该控件显示 AdventureWorksLT 数据库中的客户名称和地址列表。The page contains a GridView control that displays a list of customer names and addresses from the AdventureWorksLT database. 当用户单击某一行的 "删除" 链接时, 该RowDeleting事件的处理程序将检查用户尝试删除的行中显示的人员的姓氏。When the user clicks the Delete link for a row, the handler for the RowDeleting event checks the last name of the person displayed in the row that the user is trying to delete. 如果姓氏为 "Beaver", 则取消删除操作, 并显示一条错误消息。If the last name is "Beaver", the delete operation is canceled, and an error message is displayed. 对于任何其他名称, 删除操作将继续, 并删除该行。For any other name, the delete operation proceeds and the row is deleted.

事件处理程序使用RowIndex GridViewDeleteEventArgs对象的属性来查找用户尝试删除的行。The event handler uses the RowIndex property of the GridViewDeleteEventArgs object to find the row that the user is trying to delete. 该示例检查Rows集合的内容。The example examines the contents of the Rows collection. 如果要比较的值是键值, 可以改为检查DataKeys集合。If the value you want to compare to is a key value, you could examine the DataKeys collection instead.

将从 CustomerAddress 表而不是 Customer 表中删除行, 以便使示例简单。Rows are deleted from the CustomerAddress table instead of the Customer table in order to keep the example simple. GridView控件显示联接三个表的结果:Customer、Address 和 CustomerAddress。The GridView control shows the result of joining three tables: Customer, Address, and CustomerAddress. 删除 CustomerAddress 行后, 相应GridView的行将消失。When a CustomerAddress row is deleted, the corresponding GridView row disappears. 引用完整性约束会使实际删除 Customer 表中的行的示例代码更复杂。Referential integrity constraints would make the code for an example that actually deletes rows from the Customer table more complex.

有关如何设置 AdventureWorksLT 数据库的信息, 请参阅如何:设置用于 ASP.NET 开发的 AdventureWorksLT 示例数据库。For information about how to set up the AdventureWorksLT database, see How to: Set Up an AdventureWorksLT Sample Database for ASP.NET Development.


<%@ 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_RowDeleting
        (Object sender, GridViewDeleteEventArgs e)
    {
        TableCell cell = CustomersGridView.Rows[e.RowIndex].Cells[2];
        if (cell.Text == "Beaver")
        {
            e.Cancel = true;
            Message.Text = "You cannot delete customer Beaver.";
        }
        else
        {
            Message.Text = "";
        }
    }  

</script>

<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
    <title>GridView RowDeleting Example</title>
</head>
<body>
    <form id="form1" runat="server">
    <h3>
        GridView RowDeleting Example
    </h3>
    <asp:Label ID="Message" ForeColor="Red" runat="server" />
    <br />
    <asp:GridView ID="CustomersGridView" runat="server" 
        DataSourceID="CustomersSqlDataSource" 
        AutoGenerateColumns="False"
        AutoGenerateDeleteButton="True" 
        OnRowDeleting="CustomersGridView_RowDeleting"
        DataKeyNames="CustomerID,AddressID">
        <Columns>
            <asp:BoundField DataField="FirstName" 
                HeaderText="FirstName" SortExpression="FirstName" />
            <asp:BoundField DataField="LastName" HeaderText="LastName" 
                SortExpression="LastName" />
            <asp:BoundField DataField="City" HeaderText="City" 
                SortExpression="City" />
            <asp:BoundField DataField="StateProvince" HeaderText="State" 
                SortExpression="StateProvince" />
        </Columns>
    </asp:GridView>
    <asp:SqlDataSource ID="CustomersSqlDataSource" runat="server"
        SelectCommand="SELECT SalesLT.CustomerAddress.CustomerID, 
            SalesLT.CustomerAddress.AddressID, 
            SalesLT.Customer.FirstName, 
            SalesLT.Customer.LastName, 
            SalesLT.Address.City, 
            SalesLT.Address.StateProvince 
            FROM SalesLT.Customer 
            INNER JOIN SalesLT.CustomerAddress 
            ON SalesLT.Customer.CustomerID = 
                SalesLT.CustomerAddress.CustomerID 
            INNER JOIN SalesLT.Address ON SalesLT.CustomerAddress.AddressID = 
                SalesLT.Address.AddressID"
        DeleteCommand="Delete from SalesLT.CustomerAddress where CustomerID = 
            @CustomerID and AddressID = @AddressID" 
        ConnectionString="<%$ ConnectionStrings:AdventureWorksLTConnectionString %>">
        <DeleteParameters>
            <asp:Parameter Name="AddressID" />
            <asp:Parameter Name="CustomerID" />
        </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">

    Private Sub CustomersGridView_RowDeleting _
        (ByVal sender As [Object], _
        ByVal e As GridViewDeleteEventArgs)
        Dim cell As TableCell
        cell = CustomersGridView.Rows(e.RowIndex).Cells(2)
        If cell.Text = "Beaver" Then
            e.Cancel = True
            Message.Text = "You cannot delete customer Beaver."
        Else
            Message.Text = ""
        End If
    End Sub
    
</script>

<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
    <title>GridView RowDeleting Example</title>
</head>
<body>
    <form id="form1" runat="server">
    <h3>
        GridView RowDeleting Example
    </h3>
    <asp:Label ID="Message" ForeColor="Red" runat="server" />
    <br />
    <asp:GridView ID="CustomersGridView" runat="server" 
        DataSourceID="CustomersSqlDataSource" 
        AutoGenerateColumns="False"
        AutoGenerateDeleteButton="True" 
        OnRowDeleting="CustomersGridView_RowDeleting"
        DataKeyNames="CustomerID,AddressID">
        <Columns>
            <asp:BoundField DataField="FirstName" 
                HeaderText="FirstName" SortExpression="FirstName" />
            <asp:BoundField DataField="LastName" HeaderText="LastName" 
                SortExpression="LastName" />
            <asp:BoundField DataField="City" HeaderText="City" 
                SortExpression="City" />
            <asp:BoundField DataField="StateProvince" HeaderText="State" 
                SortExpression="StateProvince" />
        </Columns>
    </asp:GridView>
    <asp:SqlDataSource ID="CustomersSqlDataSource" runat="server"
        SelectCommand="SELECT SalesLT.CustomerAddress.CustomerID, 
            SalesLT.CustomerAddress.AddressID, 
            SalesLT.Customer.FirstName, 
            SalesLT.Customer.LastName, 
            SalesLT.Address.City, 
            SalesLT.Address.StateProvince 
            FROM SalesLT.Customer 
            INNER JOIN SalesLT.CustomerAddress 
            ON SalesLT.Customer.CustomerID = 
                SalesLT.CustomerAddress.CustomerID 
            INNER JOIN SalesLT.Address ON SalesLT.CustomerAddress.AddressID = 
                SalesLT.Address.AddressID"
        DeleteCommand="Delete from SalesLT.CustomerAddress where CustomerID = 
            @CustomerID and AddressID = @AddressID" 
        ConnectionString="<%$ ConnectionStrings:AdventureWorksLTConnectionString %>">
        <DeleteParameters>
            <asp:Parameter Name="AddressID" />
            <asp:Parameter Name="CustomerID" />
        </DeleteParameters>
    </asp:SqlDataSource>
    </form>
</body>
</html>

注解

RowDeleting单击某一行的 "删除" 按钮时, 但在GridView控件删除该行之前, 将引发该事件。The RowDeleting event is raised when a row's Delete button is clicked, but before the GridView control deletes the row. 这使你可以提供一种事件处理方法, 该方法可在每次发生此事件时执行自定义例程 (如取消删除操作)。This enables you to provide an event-handling method that performs a custom routine, such as canceling the delete operation, whenever this event occurs.

GridViewDeleteEventArgs对象传递给事件处理方法, 这使您能够确定当前行的索引, 并指示应取消删除操作。A GridViewDeleteEventArgs object is passed to the event-handling method, which enables you to determine the index of the current row and to indicate that the delete operation should be canceled. 若要取消删除操作, 请将Cancel GridViewDeleteEventArgs对象的属性设置为trueTo cancel the delete operation, set the Cancel property of the GridViewDeleteEventArgs object to true. 如果需要, 还可以KeysValues将值传递给数据源之前操作和集合。You can also manipulate the Keys and Values collections, if necessary, before the values are passed to the data source.

有关如何处理事件的详细信息, 请参阅处理和引发事件For more information about how to handle events, see Handling and Raising Events.

适用于

另请参阅