Export (0) Print
Expand All

CommandField Class

Represents a special field that displays command buttons to perform selecting, editing, inserting, or deleting operations in a data-bound control.

Namespace:  System.Web.UI.WebControls
Assembly:  System.Web (in System.Web.dll)

[AspNetHostingPermissionAttribute(SecurityAction.LinkDemand, Level = AspNetHostingPermissionLevel.Minimal)]
[AspNetHostingPermissionAttribute(SecurityAction.InheritanceDemand, Level = AspNetHostingPermissionLevel.Minimal)]
public class CommandField : ButtonFieldBase

The CommandField class is a special field used by data-bound controls (such as GridView and DetailsView) to display command buttons that perform delete, edit, insert, or select operations. The command buttons to perform these operations can be shown or hidden using the properties shown in the following table.

Property

Description

ShowDeleteButton

Shows or hides a Delete button in a CommandField field for each record in the data-bound control. The Delete button allows the user to delete a record from the data source.

ShowEditButton

Shows or hides an Edit button in a CommandField field for each record in the data-bound control. The Edit button allows the user to edit a record from the data source. When the user clicks the Edit button for a specific record, that Edit button is replaced with an Update button and a Cancel button. All other command buttons are also hidden.

ShowInsertButton

Shows or hides the New button in a CommandField field. The New button allows the user to insert a new record in the data source. When the user clicks the New button, it is replaced with an Insert button and a Cancel button. All other command buttons are also hidden.

NoteNote:

This property applies only to data-bound controls that support inserting operations, such as the DetailsView control.

ShowSelectButton

Shows or hides a Select button in a CommandField field for each record in the data-bound control. The Select button allows the user to select a record in the data-bound control.

In addition, the Cancel button displayed when a record is in edit or insert mode can be shown or hidden by setting the ShowCancelButton property.

NoteNote:

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 delete, update, and insert functionality. For other data sources, you need to provide the routines to perform these operations during the appropriate events for the data-bound control.

The CommandField field is displayed differently depending on the data-bound control in which it is used. For example, the GridView control displays a CommandField field as a column, while the DetailsView control displays it as a row.

To specify the type of button to display, use the ButtonType property. When the ButtonType property is set to ButtonType.Button or ButtonType.Link, you can specify the text to display for the buttons by setting the properties shown in the following table.

Property

Description

CancelText

The caption for the Cancel button.

DeleteText

The caption for the Delete button.

EditText

The caption for the Edit button.

InsertText

The caption for the Insert button.

NoteNote:

This property applies only to data-bound controls that support insert operations, such as the DetailsView control.

NewText

The caption for the New button.

NoteNote:

This property applies only to data-bound controls that support insert operations, such as the DetailsView control.

SelectText

The caption for the Select button.

UpdateText

The caption for the Update button.

Instead of displaying a command button or a link button, you can display an image button by setting the ButtonType property to ButtonType.Image and then setting the properties shown in the following table.

Property

Description

CancelImageUrl

The image to display for the Cancel button

DeleteImageUrl

The image to display for the Delete button.

EditImageUrl

The image to display for the Edit button.

InsertText

The image to display for the Insert button.

NoteNote:

This property applies only to data-bound controls that support insert operations, such as the DetailsView control.

NewImageUrl

The image to display for the New button.

NoteNote:

This property applies only to data-bound controls that support insert operations, such as the DetailsView control.

SelectImageUrl

The image to display for the Select button.

UpdateImageUrl

The image to display for the Update button.

By default, when the user clicks a button in a CommandField field, validation is performed on all validation controls on the page. To prevent validation from occurring when a button is clicked, set the CausesValidation property to false.

You can hide a CommandField field in a data-bound control by setting the Visible property to false.

The CommandField field allows you to customize its header and footer sections. To display a caption in the header or the footer section, set the HeaderText or the FooterText property, respectively. Instead of displaying text in the header section, you can display an image by setting the HeaderImageUrl property. To hide the header section in a CommandField object, set the ShowHeader property to false.

NoteNote:

Some data-bound controls (such as the GridView control) can show or hide only the entire header section of the control. These data-bound controls do not support the ShowHeader property for an individual button field. To show or hide the entire header section of a data-bound control (if available), use the control's ShowHeader property.

You also can customize the appearance of the CommandField object (font color, background color, and so on) by setting the style properties for the different parts of the field. The following table lists the different style properties.

Style property

Description

ControlStyle

The style settings for the child Web server controls of the CommandField object.

FooterStyle

The style settings for the footer section of the CommandField object.

HeaderStyle

The style settings for the header section of the CommandField object.

ItemStyle

The style settings for the data items in the CommandField object.

The following code example demonstrates how to use a CommandField field to display controls to edit a 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"
            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>

The following code example demonstrates how to use the CancelText, EditText, and UpdateText properties to customize the text for the editing controls.

<%@ 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"
            edittext="Edit Customer"
            canceltext="Discard"
            updatetext="Revise"
            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>

Any public static (Shared in Visual Basic) members of this type are thread safe. Any instance members are not guaranteed to be thread safe.

Windows 7, Windows Vista, Windows XP SP2, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP Starter Edition, Windows Server 2008 R2, Windows Server 2008, Windows Server 2003, Windows Server 2000 SP4, Windows Millennium Edition, Windows 98

The .NET Framework and .NET Compact Framework do not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.

.NET Framework

Supported in: 3.5, 3.0, 2.0

Community Additions

ADD
Show:
© 2014 Microsoft