DetailsView.ItemDeleting 事件

定义

在单击 DetailsView 控件中的“删除”按钮时,但在删除操作之前发生。

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

事件类型

示例

下面的代码示例演示如何在用户尝试从DetailsView控件中删除最后一项时使用 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 CustomerDetailView_ItemDeleting(Object sender, DetailsViewDeleteEventArgs e)
  {
    // Cancel the delete operation if the user attempts to delete the last
    // record from the data source.
      if (CustomerDetailView.DataItemCount <= 1)
    {
      e.Cancel = true;
      MessageLabel.Text = "You must keep at least one store.";
    }
    else
    {
      MessageLabel.Text = "";
    }
  }

</script>

<html xmlns="http://www.w3.org/1999/xhtml" >
  <head runat="server">
    <title>DetailsView ItemDeleting Example</title>
</head>
<body>
    <form id="Form1" runat="server">
        
      <h3>DetailsView ItemDeleting Example</h3>
                
        <asp:detailsview id="CustomerDetailView"
          datasourceid="DetailsViewSource"
          datakeynames="CustomerID"
          autogeneratedeletebutton="true"  
          autogeneraterows="true"
          allowpaging="true"
          onitemdeleting="CustomerDetailView_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" runat="server" 
          ConnectionString=
            "<%$ ConnectionStrings:NorthWindConnectionString%>"
            InsertCommand="INSERT INTO [Customers]([CustomerID], [CompanyName], [Address], [City], [PostalCode], [Country]) VALUES (@CustomerID, @CompanyName, @Address, @City, @PostalCode, @Country)"
            SelectCommand="Select [CustomerID], [CompanyName], 
              [Address], [City], [PostalCode], [Country] 
              From [Customers]"
            DeleteCommand="DELETE FROM [Customers] WHERE [CustomerID] = @CustomerID" >
        </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 CustomerDetailView_ItemDeleting(ByVal sender As Object, ByVal e As DetailsViewDeleteEventArgs)
        ' Cancel the delete operation if the user attempts to delete the last
        ' record from the data source.
        If (CustomerDetailView.DataItemCount <= 1) Then
            e.Cancel = True
            MessageLabel.Text = "You must keep at least one store."
        Else
            MessageLabel.Text = ""
        End If
    End Sub

</script>

<html xmlns="http://www.w3.org/1999/xhtml" >
  <head runat="server">
    <title>DetailsView ItemDeleting Example</title>
</head>
<body>
    <form id="Form1" runat="server">
        
      <h3>DetailsView ItemDeleting Example</h3>
                
        <asp:detailsview id="CustomerDetailView"
          datasourceid="DetailsViewSource"
          datakeynames="CustomerID"
          autogeneratedeletebutton="true"  
          autogeneraterows="true"
          allowpaging="true"
          onitemdeleting="CustomerDetailView_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" runat="server" 
          ConnectionString=
            "<%$ ConnectionStrings:NorthWindConnectionString%>"
            InsertCommand="INSERT INTO [Customers]([CustomerID], [CompanyName], [Address], [City], [PostalCode], [Country]) VALUES (@CustomerID, @CompanyName, @Address, @City, @PostalCode, @Country)"
            SelectCommand="Select [CustomerID], [CompanyName], 
              [Address], [City], [PostalCode], [Country] 
              From [Customers]"
            DeleteCommand="DELETE FROM [Customers] WHERE [CustomerID] = @CustomerID" >
        </asp:SqlDataSource>
    </form>
  </body>
</html>

注解

单击 ItemDeleting 控件中的 DetailsView “删除”按钮时,但在删除操作之前,将引发 该事件。 这允许你提供一个事件处理程序,用于执行自定义例程,例如,每当发生此事件时取消删除操作。

对象 DetailsViewDeleteEventArgs 将传递给事件处理程序,该处理程序允许你确定要删除的记录的索引,并指示应取消删除操作。 若要取消删除操作,请将 Cancel 属性设置为 true。 如有必要, Keys 还可以在值传递到数据源之前操作 和 Values 集合。

有关如何处理事件的详细信息,请参阅 处理和引发事件

适用于

另请参阅