DetailsView.AutoGenerateDeleteButton 属性

定义

获取或设置一个值,该值指示用来删除当前记录的内置控件是否在 DetailsView 控件中显示。Gets or sets a value indicating whether the built-in control to delete the current record is displayed in a DetailsView control.

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 表示显示用来删除当前记录的内置控件;否则为 falsetrue to display the built-in control to delete the current record; otherwise, false. 默认值为 falseThe default is false.

示例

下面的代码示例演示如何使用AutoGenerateDeleteButton属性来显示用于删除当前记录的内置控件。The following code example demonstrates how to use the AutoGenerateDeleteButton property to display the built-in control to delete the current record.


<%@ 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 true CanDelete DataSourceView当控件绑定到从继承的对象并返回属性时, 控件可以利用数据源控件的功能并提供自动DetailsView正在删除功能。When a DetailsView control is bound to an object that inherits from DataSourceView and the CanDelete property returns true, the DetailsView control can take advantage of the data source control's capabilities and provide automatic deleting functionality.

备注

对于要删除数据的SqlDataSource.DeleteCommand SqlDataSource对象,必须使用 delete 查询语句设置基础对象的属性。 SqlDataSourceViewFor a SqlDataSourceView object to delete data, the SqlDataSource.DeleteCommand property of the underlying SqlDataSource object must be set with a delete query statement.

当属性设置为true时, 将在CommandField DetailsView控件中自动显示带有 "删除" 按钮的行字段。 AutoGenerateDeleteButtonWhen the AutoGenerateDeleteButton property is set to true, a CommandField row field with a Delete button is automatically displayed in the DetailsView control. 单击 "删除" 按钮将从数据源中永久删除该记录。Clicking the Delete button permanently removes that record from the data source.

备注

还必须将 "自动DataKeyNames删除" 功能的属性设置为 "工作"。You must also set the DataKeyNames property for the automatic deletion feature to work.

DetailsView控件提供若干事件, 可用于在删除记录时执行自定义操作。The DetailsView control provides several events that you can use to perform a custom action when a record is deleted. 下表列出了可用的事件。The following table lists the available events.

事件Event 说明Description
ItemDeleted 在单击 "删除" 按钮时, 但在DetailsView控件从数据源中删除记录之后发生。Occurs when the Delete button is clicked, but after the DetailsView control deletes the record from the data source. 此事件通常用于检查删除操作的结果。This event is often used to check the results of the delete operation.
ItemDeleting 在单击 "删除" 按钮时, 但在DetailsView控件从数据源中删除记录之前发生。Occurs when the Delete button is clicked, but before the DetailsView control deletes the record from the data source. 此事件通常用于取消删除操作。This event is often used to cancel the delete operation.

的值AutoGenerateDeleteButton存储在视图状态中。The value of AutoGenerateDeleteButton is stored in view state.

适用于

另请参阅