ButtonField Class

Represents a field that is displayed as a button in a data-bound control.

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

public class ButtonField : ButtonFieldBase
public class ButtonField extends ButtonFieldBase
public class ButtonField extends ButtonFieldBase
Not applicable.

The ButtonField class is used by data-bound controls (such as GridView and DetailsView) to display a button for each record that is displayed. The ButtonField object is displayed differently depending on the data-bound control in which it is used. For example, the GridView control displays a ButtonField object as a column, while the DetailsView control displays it as a row.

Clicking a button in a button field raises the command event of the parent data-bound control. You can provide a custom routine to perform when a command button is clicked by providing an event handler for the command event.


The GridView control raises the RowCommand event, while the DetailsView control raises the ItemCommand event.

To determine the index of the record that raises the command event, use the CommandArgument property of the event argument that is passed to the command event for the data-bound control. The ButtonField class automatically populates the CommandArgument property with the appropriate index value.

To specify the type of button to display, use the ButtonType property. When displaying a link or command button, use the Text property to specify the caption to display in the buttons.


If you set the Text property, all buttons in a ButtonField share the same caption.

Alternatively, you can bind the ButtonField object to a field in a data source. This allows you to display different captions for the buttons in the ButtonField object. The values that are in the specified field are used for the text captions of the buttons. Set the DataTextField property to bind a ButtonField object to a field in a data source.

When displaying an image button, use the ImageUrl property to specify the image to display for the buttons in the ButtonField object.


All buttons in a ButtonField object share the same image.

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

The ButtonField object allows you to customize its header and footer sections. To display a caption in the header or footer section, set the HeaderText or FooterText properties, 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 ButtonField object, set the ShowHeader property to false.


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 ShowHeader property for the control.

You also can customize the appearance of the ButtonField 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

The style settings for


The child Web server controls of the ButtonField.


The footer section of the ButtonField.


The header section of the ButtonField.


The data items in the ButtonField.

The following code example demonstrates how to use a ButtonField object to display a column of command buttons in a GridView control.

<%@ Page language="C#" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
<script runat="server">

  void CustomersGridView_RowCommand(Object sender, GridViewCommandEventArgs e)
    // If multiple ButtonField column fields are used, use the
    // CommandName property to determine which button was clicked.
      // Convert the row index stored in the CommandArgument
      // property to an Integer.
      int index = Convert.ToInt32(e.CommandArgument);    
      // Get the last name of the selected author from the appropriate
      // cell in the GridView control.
      GridViewRow selectedRow = CustomersGridView.Rows[index];
      TableCell contactName = selectedRow.Cells[1];
      string contact = contactName.Text;  
      // Display the selected author.
      Message.Text = "You selected " + contact + ".";

<html xmlns="http://www.w3.org/1999/xhtml" >
  <head runat="server">
    <title>ButtonField Example</title>
    <form id="form1" runat="server">
      <h3>ButtonField Example</h3>
      <asp:label id="Message"
      <!-- Populate the Columns collection declaratively. -->
      <asp:gridview id="CustomersGridView" 
          <asp:buttonfield buttontype="Button" 
            headertext="Select Customer" 
          <asp:boundfield datafield="CompanyName" 
            headertext="Company Name"/>
          <asp:boundfield datafield="ContactName" 
            headertext="Contact Name"/>
        <!-- This example uses Microsoft SQL Server and connects -->
        <!-- to the Northwind sample database.                   -->
        <asp:sqldatasource id="CustomersSqlDataSource"  
          selectcommand="Select [CustomerID], [CompanyName], [ContactName], [ContactTitle] From [Customers]"
          connectionstring="<%$ ConnectionStrings:NorthWindConnection%>"

  • AspNetHostingPermission  for using a ButtonField object in a hosted environment. Associated enumeration value: System.Web.AspNetHostingPermissionLevel.Minimal

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 98, Windows Server 2000 SP4, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition

The Microsoft .NET Framework 3.0 is supported on Windows Vista, Microsoft Windows XP SP2, and Windows Server 2003 SP1.

.NET Framework

Supported in: 3.0, 2.0