DetailsView.AutoGenerateEditButton 属性

定义

获取或设置一个值,该值指示用来编辑当前记录的内置控件是否在 DetailsView 控件中显示。Gets or sets a value indicating whether the built-in controls to edit the current record are displayed in a DetailsView 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 表示显示用来编辑当前记录的内置控件;否则为 falsetrue to display the built-in controls to edit the current record; otherwise, false. 默认值为 falseThe default is false.

示例

下面的代码示例演示如何使用AutoGenerateEditButton属性来显示用于编辑当前记录的内置控件。The following code example demonstrates how to use the AutoGenerateEditButton property to display the built-in controls to edit 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 AutoGenerateEditButton Example</title>
</head>
<body>
    <form id="Form1" runat="server">
        
      <h3>DetailsView AutoGenerateEditButton Example</h3>
                
        <asp:detailsview id="CustomerDetailView"
          datasourceid="DetailsViewSource"
          datakeynames="CustomerID"
          autogenerateeditbutton="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 AutoGenerateEditButton Example</title>
</head>
<body>
    <form id="Form1" runat="server">
        
      <h3>DetailsView AutoGenerateEditButton Example</h3>
                
        <asp:detailsview id="CustomerDetailView"
          datasourceid="DetailsViewSource"
          datakeynames="CustomerID"
          autogenerateeditbutton="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控件时, 该DetailsView控件可以利用数据源控件的功能并提供自动更新功能。When a data source control that supports updating is bound to a DetailsView control, the DetailsView control can take advantage of the data source control's capabilities and provide automatic updating functionality.

备注

对于用于更新数据的数据源控件, 必须SqlDataSource.UpdateCommand使用 update 查询语句设置其属性。For a data source control to update data, its SqlDataSource.UpdateCommand property must be set with an update query statement.

当属性设置为true时, 将在CommandField DetailsView控件中自动显示带有 "编辑" 按钮的行字段。 AutoGenerateEditButtonWhen the AutoGenerateEditButton property is set to true, a CommandField row field with an Edit button is automatically displayed in the DetailsView control. 单击 "编辑" 按钮会DetailsView将该控件置于编辑模式。Clicking the Edit button puts that DetailsView control in edit mode. 处于编辑模式时, 控件中不是只读的每个绑定字段都会显示相应的输入控件 ( TextBox如控件) 作为字段的数据类型。When in edit mode, each bound field in the control 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. 单击 "更新" 按钮可使用任何值更改更新数据源中的记录, 并将该控件返回到由DefaultMode属性指定的模式。Clicking the Update button updates the record in the data source with any value changes and returns the control to the mode specified by the DefaultMode property. 单击 "取消" 按钮将放弃任何值更改, 并将控件返回到默认模式。Clicking the Cancel button abandons any value changes and returns the control to the default mode.

备注

若要以DetailsView编程方式将控件置于编辑模式, ChangeMode请使用方法。To put a DetailsView control in edit mode programmatically, use the ChangeMode method.

当属性设置为true时, DetailsView控件将自动确保在DataKeyNames属性中指定的一个或多个字段是只读的。 AutoGenerateRowsWhen the AutoGenerateRows property is also set to true, the DetailsView control automatically ensures that the field or fields specified in the DataKeyNames property are read-only.

备注

除非您实现自己的更新功能, 否则必须将自动DataKeyNames更新功能的属性设置为 "工作"。Unless you implement your own updating functionality, you must set the DataKeyNames property for the automatic updating feature to work.

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

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

事件Event 说明Description
ItemUpdated 在单击 "更新" 按钮时, 但在DetailsView控件更新记录后发生。Occurs when the Update button is clicked, but after the DetailsView control updates the record. 此事件通常用于检查更新操作的结果。This event is often used to check the results of the update operation.
ItemUpdating 在单击 "更新" 按钮时, 但在DetailsView控件更新记录之前发生。Occurs when the Update button is clicked, but before the DetailsView control updates the record. 此事件通常用于取消更新操作。This event is often used to cancel the update operation.
ModeChanged DetailsView控件更改模式之后发生。Occurs after the DetailsView control changes modes.
ModeChanging DetailsView控件更改模式之前发生。Occurs before the DetailsView control changes modes. 此事件通常用于取消模式更改。This event is often used to cancel the mode change.

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

适用于

另请参阅