Bearbeiten

Freigeben über


DetailsView.AutoGenerateEditButton Property

Definition

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

Property Value

true to display the built-in controls to edit the current record; otherwise, false. The default is false.

Examples

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>

Remarks

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.

Note

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

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

Note

To put a DetailsView control in edit mode programmatically, use the ChangeMode method.

When 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.

Note

Unless you implement your own updating functionality, you must set the DataKeyNames property for the automatic updating feature to work.

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.

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 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 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 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 AutoGenerateEditButton is stored in view state.

Applies to

See also