DetailsView.AutoGenerateInsertButton Property

Definition

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

Property Value

true to display the built-in controls to insert a new record; otherwise, false. The default is false.

Examples

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>

Remarks

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.

Note

For a data source control to insert data, its SqlDataSource.InsertCommand property must be set with an insert query statement.

When the AutoGenerateInsertButton property is set to true, a CommandField row field with a New button is automatically displayed in the DetailsView control. Clicking the New button puts that DetailsView control in insert mode. 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. 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.

Note

To put a row in insert mode programmatically, use the ChangeMode method.

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.

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 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 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 Occurs after the DetailsView control changes modes.
ModeChanging Occurs before the DetailsView control changes modes. This event is often used to cancel the mode change.

The value of AutoGenerateInsertButton is stored in view state.

Applies to

See also