DetailsView.AutoGenerateInsertButton 属性

定义

获取或设置一个值,该值指示用来插入新记录的内置控件是否在 DetailsView 控件中显示。Gets or sets a value indicating whether the built-in controls to insert a new record are displayed in a DetailsView control.

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

属性值

true 表示显示用来插入新记录的内置控件;否则为 falsetrue to display the built-in controls to insert a new record; otherwise, false. 默认值为 falseThe default is false.

示例

下面的代码示例演示如何使用AutoGenerateInsertButton属性来显示内置控件, 以便DetailsView在控件中插入新记录。The following code example demonstrates how to use the AutoGenerateInsertButton property to display the built-in controls to insert a new record in a DetailsView control.


<%@ 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 AutoGenerateInsertButton Example</title>
</head>
<body>
    <form id="Form1" runat="server">
        
      <h3>DetailsView AutoGenerateInsertButton Example</h3>
                
        <asp:detailsview id="CustomerDetailView"
          datasourceid="DetailsViewSource"
          datakeynames="CustomerID"
          autogenerateinsertbutton="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 AutoGenerateInsertButton Example</title>
</head>
<body>
    <form id="Form1" runat="server">
        
      <h3>DetailsView AutoGenerateInsertButton Example</h3>
                
        <asp:detailsview id="CustomerDetailView"
          datasourceid="DetailsViewSource"
          datakeynames="CustomerID"
          autogenerateinsertbutton="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 inserting is bound to a DetailsView control, the DetailsView control can take advantage of the data source control's capabilities and provide automatic inserting functionality.

备注

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

当属性设置为true时, 将在CommandField DetailsView控件中自动显示带有 "新建" 按钮的行字段。 AutoGenerateInsertButtonWhen the AutoGenerateInsertButton property is set to true, a CommandField row field with a New button is automatically displayed in the DetailsView control. 单击 "新建" 按钮会DetailsView将该控件置于插入模式下。Clicking the New button puts that DetailsView control in insert mode. 处于插入模式时, 控件中不是只读的每个绑定字段都会显示适当的输入控件 (如TextBox控件) 作为字段的数据类型。When in insert 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 enter the field's value for the new record.

单击 "新建" 按钮时, 还会将其替换为 "插入" 按钮和 "取消" 按钮。When clicked, the New button is also replaced with an Insert button and a Cancel button. 单击 "插入" 按钮会在数据源中插入新记录, 并将该控件返回到由DefaultMode属性指定的模式。Clicking the Insert button inserts the new record in the data source and returns the control to the mode specified by the DefaultMode property. 单击 "取消" 按钮将放弃插入操作, 并将控件返回到默认模式。Clicking the Cancel button abandons the insert operation and returns the control to the default mode.

备注

若要以编程方式将一行置于插入模式, ChangeMode请使用方法。To put a row in insert mode programmatically, use the ChangeMode method.

您可以使用InsertRowStyle属性控制处于插入模式的记录的外观。You can control the appearance of a record that is in insert mode by using the InsertRowStyle 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 new record is inserted. 下表列出了可用的事件。The following table lists the available events.

事件Event 说明Description
ItemInserted 单击 "插入" 按钮时, 但在DetailsView控件插入记录后发生。Occurs when the Insert button is clicked, but after the DetailsView control inserts the record. 此事件通常用于检查插入操作的结果。This event is often used to check the results of the insert operation.
ItemInserting 在单击 "插入" 按钮时, 但在DetailsView控件插入记录之前发生。Occurs when the Insert button is clicked, but before the DetailsView control inserts the record. 此事件通常用于取消插入操作。This event is often used to cancel the insert 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.

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

适用于

另请参阅