This documentation is archived and is not being maintained.

ButtonField.DataTextFormatString Property

Gets or sets the string that specifies the display format for the value of the field.

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

public virtual string DataTextFormatString { get; set; }

Property Value

Type: System.String
A format string that specifies the display format for the value of the field. The default is an empty string (""), which indicates that no special formatting is applied to the field value.

Use the DataTextFormatString property to specify a custom display format for the values that are displayed in the ButtonField object. If the DataTextFormatString property is not set, the value for the field is displayed without any special formatting.

NoteNote

The format string is applied only when the DataTextField property is set.

The format string can be any literal string and usually includes a placeholder for the value for the field. For example, in the format string "Item Value: {0}", the {0} placeholder is replaced with the value for the field when it is displayed in the ButtonField object. The rest of the format string is displayed as literal text.

NoteNote

If the format string does not include a placeholder, the value for the field from the data source is not included in the final display text.

The placeholder consists of two parts, separated by a colon and wrapped in braces, in the form { A : Bxx }. The value before the colon (A in the general example) specifies the index for the field value in a zero-based list of parameters.

NoteNote

This A parameter is part of the formatting syntax. Because there is only one field value in each cell, this value can only be set to 0.

The colon and the values after the colon are optional. The character after the colon (B in the general example) specifies the format in which to display the value. The following table lists the common formats.

Format character

Description

C

Displays numeric values in currency format.

D

Displays numeric values in decimal format.

E

Displays numeric values in scientific (exponential) format.

F

Displays numeric values in fixed format.

G

Displays numeric values in general format.

N

Displays numeric values in number format.

X

Displays numeric values in hexadecimal format.

NoteNote

Except for X, the format characters are not case sensitive. The X format character displays the hexadecimal characters in the case that is specified.

The value after the format character (xx in the general example) specifies the number of significant digits or decimal places to display. For example, the format string "{0:F2}" displays a fixed-point number with two decimal places.

For more information on formatting strings, see Formatting Types.

The value of this property is stored in view state.

The following code example demonstrates how to use the DataTextFormatString property to specify a custom display format for the values of a field.



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

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

    }

  }

</script>

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

      <h3>ButtonField DataTextField Example</h3>

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

      <!-- Set the DataTextField property of the ButtonField -->
      <!-- declaratively. Set the DataTextFormatString       -->
      <!-- property to apply special formatting to the text. -->
      <asp:gridview id="AuthorsGridView" 
        datasourceid="AuthorsSqlDataSource" 
        autogeneratecolumns="false"
        onrowcommand="AuthorsGridView_RowCommand" 
        runat="server">

        <columns>

          <asp:buttonfield buttontype="Link" 
            commandname="Select"
            headertext="Select Author"
            datatextfield="au_lname"
            datatextformatstring="[{0}]"    
            text="Select"/>
          <asp:boundfield datafield="au_fname" 
            headertext="First Name"/>

        </columns>

      </asp:gridview>

      <!-- 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"
        runat="server">
      </asp:sqldatasource>

    </form>
  </body>
</html>



.NET Framework

Supported in: 4, 3.5, 3.0, 2.0

Windows 7, Windows Vista SP1 or later, Windows XP SP3, Windows XP SP2 x64 Edition, Windows Server 2008 (Server Core not supported), Windows Server 2008 R2 (Server Core supported with SP1 or later), Windows Server 2003 SP2

The .NET Framework does not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.
Show: