GridView.AutoGenerateEditButton GridView.AutoGenerateEditButton GridView.AutoGenerateEditButton GridView.AutoGenerateEditButton Property

定义

获取或设置一个值,该值指示每个数据行都带有“编辑”按钮的 CommandField 字段列是否自动添加到 GridView 控件。Gets or sets a value indicating whether a CommandField field column with an Edit button for each data row is automatically added to a GridView control.

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

属性值

true 表示自动添加在每个数据行都带有“编辑”按钮的 CommandField 字段列;否则为 falsetrue to automatically add a CommandField field column with an Edit button for each data row; otherwise, false. 默认值为 falseThe default is false.

示例

下面的示例演示如何使用AutoGenerateEditButton属性启用GridView控件的自动编辑功能。The following example demonstrates how to use the AutoGenerateEditButton property to enable the automatic editing feature of the 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 updating is bound to a GridView control, the GridView control can take advantage of the data source control's capabilities and provide automatic updating functionality.

备注

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

当属性设置为trueCommandField ,GridView每个数据行都带有 "编辑" 按钮的列 (表示为对象) 将自动添加到控件中。 AutoGenerateEditButtonWhen the AutoGenerateEditButton property is set to true, a column (represented by a CommandField object) with an Edit button for each data row is automatically added to the GridView control. 单击某一行的 "编辑" 按钮将该行置于编辑模式。Clicking an Edit button for a row puts that row in edit mode. 如果行处于编辑模式, 则行中非只读的每个列字段都将为该字段的数据类型显示适当的输入控件TextBox , 例如控件。When a row is in edit mode, each column field in the row that is not read-only displays the appropriate input control, such as a TextBox control, for the field's data type. 这允许用户修改字段的值。This allows the user to modify the field's value.

单击 "编辑" 按钮后, 还会将其替换为 "更新" 按钮和 "取消" 按钮。When clicked, the Edit button is also replaced with an Update button and a Cancel button. 单击 "更新" 按钮可更新数据源中的行, 并返回任何值, 并将该行返回到显示模式。Clicking the Update button updates the row in the data source with any value changes and returns the row to display mode. 单击 "取消" 按钮将放弃任何值更改, 并将该行返回到显示模式。Clicking the Cancel button abandons any value changes and returns the row to display mode.

备注

您可以通过使用行的索引设置EditIndex属性, 以编程方式将行置于编辑模式。You can programmatically put a row in edit mode by setting the EditIndex property with the index of the row. 若要以编程方式退出编辑模式EditIndex , 请将属性设置为-1。To programmatically exit edit mode, set the EditIndex property to -1.

使用内置的更新功能时, 必须使用以逗号分隔的DataKeyNames字段名称列表来设置属性, 以标识数据源的主键字段, 否则, 内置的更新功能将无法更新正确的记录。When using the built-in updating functionality, you must set the DataKeyNames property with a comma-separated list of field names to identify the primary key field or fields of the data source; otherwise, the built-in updating functionality will not be able to update the correct record. 当使用自动生成的字段列 (通过将AutoGenerateColumns属性设置true为) 时GridView , 控件将自动确保自动生成的字段列对应于中指定的一个或多个字段。DataKeyNames属性为只读。When using automatically generated field columns (by setting the AutoGenerateColumns property to true), the GridView control automatically ensures that the automatically generated field columns that correspond to the field or fields specified in the DataKeyNames property are read-only.

您可以使用EditRowStyle属性控制处于编辑模式的行的外观。You can control the appearance of a row that is in edit mode by using the EditRowStyle property. 常见设置通常包括自定义背景色、前景色和字体属性。Common settings usually include a custom background color, foreground color, and font properties.

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

事件Event 说明Description
RowCancelingEdit 在单击某一行的 "取消" 按钮时, 但在GridView控件取消编辑模式之前发生。Occurs when a row's Cancel button is clicked, but before the GridView control cancels out of edit mode. 此事件通常用于停止取消操作。This event is often used to stop the canceling operation.
RowEditing 发生在单击某一行的“编辑”按钮以后,GridView 控件进入编辑模式之前。Occurs when a row's Edit button is clicked, but before the GridView control enters edit mode. 此事件通常用于取消编辑操作。This event is often used to cancel the editing operation.
RowUpdated 发生在单击某一行的“更新”按钮,并且 GridView 控件对该行进行更新之后。Occurs when a row's Update button is clicked, but after the GridView control updates the row. 此事件通常用于检查更新操作的结果。This event is often used to check the results of the update operation.
RowUpdating 发生在单击某一行的“更新”按钮以后,GridView 控件对该行进行更新之前。Occurs when a row's Update button is clicked, but before the GridView control updates the row. 此事件通常用于取消更新操作。This event is often used to cancel the updating operation.

适用于

另请参阅