Was this page helpful?
Your feedback about this content is important. Let us know what you think.
Additional feedback?
1500 characters remaining
Export (0) Print
Expand All

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)

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

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.

NoteNote:

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.

NoteNote:

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.

NoteNote:

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.

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

ControlStyle

The child Web server controls of the ButtonField.

FooterStyle

The footer section of the ButtonField.

HeaderStyle

The header section of the ButtonField.

ItemStyle

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" 
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<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. 
    if(e.CommandName=="Select")
    {

      // 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 + ".";

    }

  }

</script>

<html xmlns="http://www.w3.org/1999/xhtml" >
  <head runat="server">
    <title>ButtonField Example</title>
</head>
<body>
    <form id="form1" runat="server">

      <h3>ButtonField Example</h3>

      <asp:label id="Message"
        forecolor="Red"
        runat="server"
        AssociatedControlID="CustomersGridView"/>

      <!-- Populate the Columns collection declaratively. -->
      <asp:gridview id="CustomersGridView" 
        datasourceid="CustomersSqlDataSource" 
        autogeneratecolumns="false"
        onrowcommand="CustomersGridView_RowCommand"
        runat="server">

        <columns>

          <asp:buttonfield buttontype="Button" 
            commandname="Select"
            headertext="Select Customer" 
            text="Select"/>
          <asp:boundfield datafield="CompanyName" 
            headertext="Company Name"/>
          <asp:boundfield datafield="ContactName" 
            headertext="Contact Name"/>

        </columns>

      </asp:gridview>

        <!-- 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%>"
          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:
© 2015 Microsoft