CommandField.ShowEditButton Property
Definition
Important
Some information relates to prerelease product that may be substantially modified before it’s released. Microsoft makes no warranties, express or implied, with respect to the information provided here.
Gets or sets a value indicating whether an Edit button is displayed in a CommandField field.
public:
virtual property bool ShowEditButton { bool get(); void set(bool value); };
public virtual bool ShowEditButton { get; set; }
member this.ShowEditButton : bool with get, set
Public Overridable Property ShowEditButton As Boolean
Property Value
true
to display an Edit button in a CommandField; otherwise, false
. The default is false
.
Examples
The following code example demonstrates how to use the ShowEditButton property to display an Edit button for each record in a GridView 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>CommandField Example</title>
</head>
<body>
<form id="form1" runat="server">
<h3>CommandField Example</h3>
<asp:gridview id="CustomersGridView"
datasourceid="CustomersSqlDataSource"
autogeneratecolumns="false"
datakeynames="CustomerID"
runat="server">
<columns>
<asp:commandfield showeditbutton="true"
buttontype="Image"
editimageurl="~\Images\EditButton.jpg"
cancelimageurl="~\Images\CancelButton.jpg"
updateimageurl="~\Images\UpdateButton.jpg"
headertext="Edit Controls"/>
<asp:boundfield datafield="CustomerID"
headertext="Customer ID" />
<asp:boundfield datafield="CompanyName"
headertext="Company Name"/>
<asp:boundfield datafield="Address"
headertext="Address"/>
<asp:boundfield datafield="City"
headertext="City"/>
<asp:boundfield datafield="PostalCode"
headertext="ZIP Code"/>
<asp:boundfield datafield="Country"
headertext="Country"/>
</columns>
</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)"
connectionstring="<%$ ConnectionStrings:NorthWindConnectionString%>"
runat="server">
</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>CommandField Example</title>
</head>
<body>
<form id="form1" runat="server">
<h3>CommandField Example</h3>
<asp:gridview id="CustomersGridView"
datasourceid="CustomersSqlDataSource"
autogeneratecolumns="false"
datakeynames="CustomerID"
runat="server">
<columns>
<asp:commandfield showeditbutton="true"
buttontype="Image"
editimageurl="~\Images\EditButton.jpg"
cancelimageurl="~\Images\CancelButton.jpg"
updateimageurl="~\Images\UpdateButton.jpg"
headertext="Edit Controls"/>
<asp:boundfield datafield="CustomerID"
headertext="Customer ID" />
<asp:boundfield datafield="CompanyName"
headertext="Company Name"/>
<asp:boundfield datafield="Address"
headertext="Address"/>
<asp:boundfield datafield="City"
headertext="City"/>
<asp:boundfield datafield="PostalCode"
headertext="ZIP Code"/>
<asp:boundfield datafield="Country"
headertext="Country"/>
</columns>
</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)"
connectionstring="<%$ ConnectionStrings:NorthWindConnectionString%>"
runat="server">
</asp:sqldatasource>
</form>
</body>
</html>
Remarks
Use the ShowEditButton property to specify whether an Edit button is displayed in the CommandField field for each record in the data-source control. The Edit button allows you to edit the values of a record.
When the user clicks an Edit button, input controls are displayed for each field in the record. The Edit button for the record is replaced with an Update button and a Cancel button, and all other command buttons for the record are hidden. Clicking the Update button updates the record with the new values in the data source, whereas clicking the Cancel button cancels the operation.
Note
When a data-bound control is used in combination with a data source control (such as a SqlDataSource control), the data-bound control can take advantage of the data source control's capabilities and provide automatic updating functionality. For other data sources, you must provide the routines to perform the update operation during the appropriate event for the data-bound control.
When the ButtonType property of a CommandField field is set to ButtonType.Button
or ButtonType.Link
, use the EditText property to specify the text to display for an Edit button. Alternatively, you can display an image by first setting the ButtonType property to ButtonType.Image
and then setting the EditImageUrl property.