Assembly: System.Web (in system.web.dll)
/** @property */ public boolean get_AutoGenerateEditButton () /** @property */ public void set_AutoGenerateEditButton (boolean value)
public function get AutoGenerateEditButton () : boolean public function set AutoGenerateEditButton (value : boolean)
Property Valuetrue to automatically add a CommandField field column with an Edit button for each data row; otherwise, false. The default is false.
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.
When 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. 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.
You can programmatically put a row in edit mode by setting the EditIndex property with the index of the row. To programmatically exit edit mode, set the EditIndex property to -1.
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. 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.
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.
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.
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.
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.
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.
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.
The following code example demonstrates how to use the AutoGenerateEditButton property to enable the automatic editing feature of the GridView control.
<%@ Page language="C#" %> <html> <body> <form runat="server"> <h3>GridView Edit Example</h3> <!-- The GridView control automatically sets the columns --> <!-- specified in the datakeynames property as read-only. --> <!-- No input controls are rendered for these columns in --> <!-- edit mode. --> <asp:gridview id="CustomersGridView" datasourceid="CustomersSqlDataSource" autogeneratecolumns="true" autogeneratedeletebutton="true" autogenerateeditbutton="true" datakeynames="CustomerID" runat="server"> </asp:gridview> <!-- 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="CustomersSqlDataSource" selectcommand="Select [CustomerID], [CompanyName], [Address], [City], [PostalCode], [Country] From [Customers]" updatecommand="Update Customers SET CompanyName=@CompanyName, Address=@Address, City=@City, PostalCode=@PostalCode, Country=@Country WHERE (CustomerID = @CustomerID)" deletecommand="Delete from Customers where CustomerID = @CustomerID" connectionstring="<%$ ConnectionStrings:NorthWindConnectionString%>" runat="server"> </asp:sqldatasource> </form> </body> </html>
Windows 98, Windows 2000 SP4, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition
The .NET Framework does not support all versions of every platform. For a list of the supported versions, see System Requirements.