ButtonField.DataTextField Property


The .NET API Reference documentation has a new home. Visit the .NET API Browser on docs.microsoft.com to see the new experience.

Gets or sets the name of the data field for which the value is bound to the Text property of the Button control that is rendered by the ButtonField object.

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

[TypeConverterAttribute("System.Web.UI.Design.DataSourceViewSchemaConverter, System.Design, Version=, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a")]
public virtual string DataTextField { get; set; }

Property Value

Type: System.String

The name of the field to bind to the ButtonField. The default is an empty string (""), which indicates that the DataTextField property is not set.

When the ButtonType property of a ButtonField object is set to the Button or Link value, use the DataTextField property to specify the name of the field to bind to the ButtonField object. The values of the field are then displayed as the text for the buttons in the ButtonField object. Optionally, you can format the displayed text by setting the DataTextFormatString property.

Instead of using the DataTextField property to bind a ButtonField object to a field from a data source, you can use the Text property to display static text for the text of the buttons in the ButtonField object. When the Text property is used, each button shares the same caption.


If the DataTextField and Text properties are both set, the DataTextField property takes precedence.

The value of this property is stored in view state.

The following code example demonstrates how to use the DataTextField property to specify which field to display in a ButtonField column of a GridView control.

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

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

  void AuthorsGridView_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 = AuthorsGridView.Rows[index];
      TableCell lastNameCell = selectedRow.Cells[1];
      string lastName = lastNameCell.Text;  

      // Display the selected author.
      Message.Text = "You selected " + lastName + ".";




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

      <h3>ButtonField DataTextField Example</h3>

      <asp:label id="Message"

      <!-- Set the DataTextField property of the ButtonField -->
      <!-- declaratively. Set the DataTextFormatString       -->
      <!-- property to apply special formatting to the text. -->
      <asp:gridview id="AuthorsGridView" 


          <asp:buttonfield buttontype="Link" 
            headertext="Select Author"
          <asp:boundfield datafield="au_fname" 
            headertext="First Name"/>



      <!-- This example uses Microsoft SQL Server and connects -->
      <!-- to the Pubs sample database.                        -->
      <asp:sqldatasource id="AuthorsSqlDataSource"  
        selectcommand="SELECT [au_lname], [au_fname] FROM [authors]"
        connectionstring="server=localhost;database=pubs;integrated security=SSPI"


.NET Framework
Available since 2.0
Return to top