DetailsView.AutoGenerateDeleteButton 属性

定义

获取或设置一个值,该值指示用来删除当前记录的内置控件是否在 DetailsView 控件中显示。

public:
 virtual property bool AutoGenerateDeleteButton { bool get(); void set(bool value); };
public virtual bool AutoGenerateDeleteButton { get; set; }
member this.AutoGenerateDeleteButton : bool with get, set
Public Overridable Property AutoGenerateDeleteButton As Boolean

属性值

true 表示显示用来删除当前记录的内置控件;否则为 false。 默认值为 false

示例

下面的代码示例演示如何使用 AutoGenerateDeleteButton 属性显示内置控件以删除当前记录。


<%@ Page language="C#" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
  <head runat="server">
    <title>DetailsView AutoGenerateDeleteButton Example</title>
</head>
<body>
    <form id="Form1" runat="server">
        
      <h3>DetailsView AutoGenerateDeleteButton Example</h3>
                
        <asp:detailsview id="CustomersDetailView"
          datasourceid="DetailsViewSource"
          datakeynames="CustomerID"
          autogeneratedeletebutton="true"  
          autogeneraterows="true"
          allowpaging="true"  
          runat="server">
               
          <headerstyle backcolor="Navy"
            forecolor="White"/>
                    
        </asp:detailsview>
        
        <!-- 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]">
        </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">
<html xmlns="http://www.w3.org/1999/xhtml" >
  <head runat="server">
    <title>DetailsView AutoGenerateDeleteButton Example</title>
</head>
<body>
    <form id="Form1" runat="server">
        
      <h3>DetailsView AutoGenerateDeleteButton Example</h3>
                
        <asp:detailsview id="CustomersDetailView"
          datasourceid="DetailsViewSource"
          datakeynames="CustomerID"
          autogeneratedeletebutton="true"  
          autogeneraterows="true"
          allowpaging="true"  
          runat="server">
               
          <headerstyle backcolor="Navy"
            forecolor="White"/>
                    
        </asp:detailsview>
        
        <!-- 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]">
        </asp:SqlDataSource>
    </form>
  </body>
</html>

注解

当控件 DetailsView 绑定到继承自 DataSourceView 的对象并且 CanDelete 属性返回 true时,该 DetailsView 控件可以利用数据源控件的功能并提供自动删除功能。

注意

SqlDataSourceView对于要删除数据的对象,SqlDataSource.DeleteCommand必须使用 delete 查询语句设置基础SqlDataSource对象的 属性。

当 属性 AutoGenerateDeleteButton 设置为 true时, CommandField 控件中会自动显示具有“删除”按钮的 DetailsView 行字段。 单击“删除”按钮将从数据源中永久删除该记录。

注意

还必须设置 属性才能 DataKeyNames 使自动删除功能正常工作。

控件 DetailsView 提供多个事件,可用于在删除记录时执行自定义操作。 下表列出了可用的事件。

事件 说明
ItemDeleted 在单击“删除”按钮时发生,但在控件从数据源中删除记录之后 DetailsView 发生。 此事件通常用于检查删除操作的结果。
ItemDeleting 在单击“删除”按钮时,但在控件从数据源中删除记录之前 DetailsView 发生。 此事件通常用于取消删除操作。

的值 AutoGenerateDeleteButton 存储在视图状态中。

适用于

另请参阅