GridView.AutoGenerateDeleteButton 属性

定义

获取或设置一个值,该值指示每个数据行都带有“删除”按钮的 CommandField 字段列是否自动添加到 GridView 控件。Gets or sets a value indicating whether a CommandField field column with a Delete button for each data row is automatically added to a GridView 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 表示自动添加在每个数据行都带有“删除”按钮的 CommandField 字段列;否则为 falsetrue to automatically add a CommandField field column with a Delete button for each data row; otherwise, false. 默认值为 falseThe default is false.

示例

下面的示例演示如何使用AutoGenerateDeleteButton属性启用GridView控件的自动删除功能。The following example demonstrates how to use the AutoGenerateDeleteButton property to enable the automatic deleting feature of a GridView control.

<asp:LinqDataSource ID="LinqDataSource1" runat="server" 
    ContextTypeName="AdventureWorksLTDataClassesDataContext"
    EnableDelete="True" EnableInsert="True" EnableUpdate="True" 
    TableName="SalesOrderDetails">
</asp:LinqDataSource>

<asp:GridView ID="GridView1" runat="server" 
    AutoGenerateColumns="False" 
    DataKeyNames="SalesOrderID,SalesOrderDetailID"
    DataSourceID="LinqDataSource1">
    <Columns>
        <asp:CommandField ShowDeleteButton="True" 
            ShowEditButton="True" />
        <asp:BoundField DataField="SalesOrderID" 
            HeaderText="SalesOrderID" ReadOnly="True"
            SortExpression="SalesOrderID" />
        <asp:BoundField DataField="SalesOrderDetailID" 
            HeaderText="SalesOrderDetailID" InsertVisible="False"
            ReadOnly="True" SortExpression="SalesOrderDetailID" />
        <asp:BoundField DataField="OrderQty" 
            HeaderText="OrderQty" SortExpression="OrderQty" />
        <asp:BoundField DataField="ProductID" 
            HeaderText="ProductID" SortExpression="ProductID" />
        <asp:BoundField DataField="UnitPrice" 
            HeaderText="UnitPrice" SortExpression="UnitPrice" />
        <asp:BoundField DataField="ModifiedDate" 
            HeaderText="ModifiedDate" SortExpression="ModifiedDate" />
    </Columns>
</asp:GridView>
<asp:LinqDataSource ID="LinqDataSource1" runat="server" 
    ContextTypeName="AdventureWorksLTDataClassesDataContext"
    EnableDelete="True" EnableInsert="True" EnableUpdate="True" 
    TableName="SalesOrderDetails">
</asp:LinqDataSource>

<asp:GridView ID="GridView1" runat="server" 
    AutoGenerateColumns="False" 
    DataKeyNames="SalesOrderID,SalesOrderDetailID"
    DataSourceID="LinqDataSource1">
    <Columns>
        <asp:CommandField ShowDeleteButton="True" 
            ShowEditButton="True" />
        <asp:BoundField DataField="SalesOrderID" 
            HeaderText="SalesOrderID" ReadOnly="True"
            SortExpression="SalesOrderID" />
        <asp:BoundField DataField="SalesOrderDetailID" 
            HeaderText="SalesOrderDetailID" InsertVisible="False"
            ReadOnly="True" SortExpression="SalesOrderDetailID" />
        <asp:BoundField DataField="OrderQty" 
            HeaderText="OrderQty" SortExpression="OrderQty" />
        <asp:BoundField DataField="ProductID" 
            HeaderText="ProductID" SortExpression="ProductID" />
        <asp:BoundField DataField="UnitPrice" 
            HeaderText="UnitPrice" SortExpression="UnitPrice" />
        <asp:BoundField DataField="ModifiedDate" 
            HeaderText="ModifiedDate" SortExpression="ModifiedDate" />
    </Columns>
</asp:GridView>

注解

当支持删除的数据源控件绑定到GridView控件时, 该GridView控件可以利用数据源控件的功能并提供自动删除功能。When a data source control that supports deleting is bound to a GridView control, the GridView control can take advantage of the data source control's capabilities and provide automatic deleting functionality.

备注

对于用于删除数据的数据源控件, 必须将其配置为删除数据。For a data source control to delete data, it must be configured to delete data. 若要配置数据源控件以删除记录, 请参阅特定数据源控件的文档。To configure a data source control to delete records, see the documentation for the specific data source control.

当属性设置为trueCommandField ,GridView每个数据行的 "删除" 按钮将自动添加到控件中。 AutoGenerateDeleteButtonWhen the AutoGenerateDeleteButton property is set to true, a column (represented by a CommandField object) with a Delete button for each data row is automatically added to the GridView control. 单击某一行的 "删除" 按钮可从数据源中永久删除该记录。Clicking the Delete button for a row permanently removes that record from the data source.

备注

您还必须将DataKeyNames属性设置为标识数据源的一个或多个键字段, 才能使自动删除功能工作。You must also set the DataKeyNames property to identify the key field or fields of the data source for the automatic deleting feature to work.

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

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

适用于

另请参阅