Share via


ButtonField.DataTextFormatString 属性

定义

获取或设置字符串,该字符串指定字段值的显示格式。

public:
 virtual property System::String ^ DataTextFormatString { System::String ^ get(); void set(System::String ^ value); };
public virtual string DataTextFormatString { get; set; }
member this.DataTextFormatString : string with get, set
Public Overridable Property DataTextFormatString As String

属性值

指定字段值显示格式的格式字符串。 默认值为空字符串 (""),表示尚无特殊格式设置应用于该字段值。

示例

下面的代码示例演示如何使用 DataTextFormatString 属性为字段的值指定自定义显示格式。


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

<%@ Page language="VB" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<script runat="server">

  Sub AuthorsGridView_RowCommand(ByVal sender As Object, ByVal e As GridViewCommandEventArgs)
  
    ' If multiple ButtonField column fields are used, use the
    ' CommandName property to determine which button was clicked.
    If e.CommandName = "Select" Then
    
      ' Convert the row index stored in the CommandArgument
      ' property to an Integer.
      Dim index As Integer = Convert.ToInt32(e.CommandArgument)
    
      ' Get the last name of the selected author from the appropriate
      ' cell in the GridView control.
      Dim selectedRow As GridViewRow = AuthorsGridView.Rows(index)
      Dim lastNameCell As TableCell = selectedRow.Cells(1)
      Dim lastName As String = lastNameCell.Text
    
      ' Display the selected author.
      Message.Text = "You selected " & lastName & "."
      
    End If
    
  End Sub
    
</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>

注解

DataTextFormatString使用 属性为对象中显示的ButtonField值指定自定义显示格式。 DataTextFormatString如果未设置 属性,则显示字段的值没有任何特殊格式。

注意

仅当设置 属性时, DataTextField 才会应用格式字符串。

格式字符串可以是任何文本字符串,通常包括字段值的占位符。 例如,在格式字符串 "Item Value: {0}"中, {0} 占位符在 对象中 ButtonField 显示时,将替换为字段的值。 格式字符串的其余部分显示为文本文本。

注意

如果格式字符串不包含占位符,则数据源中字段的值不会包含在最终显示文本中。

占位符由两个部分组成,用冒号分隔,用大括号包装,格式为 { ABxx }。 常规示例中冒号 (A 的值) 指定从零开始的参数列表中的字段值的索引。

注意

此参数 A 是格式设置语法的一部分。 由于每个单元格中只有一个字段值,因此此值只能设置为 0。

冒号和冒号后面的值是可选的。 常规示例中冒号后 (B 字符) 指定显示值的格式。 下表列出了常用格式。

设置字符格式 说明
C 以货币格式显示数值。
D 以十进制格式显示数值。
E 以科学 (指数) 格式显示数值。
F 以固定格式显示数值。
G 以常规格式显示数值。
N 以数字格式显示数值。
X 以十六进制格式显示数值。

注意

X外,格式字符不区分大小写。 格式 X 字符在指定的情况下显示十六进制字符。

常规示例中格式字符 (xx 的值) 指定要显示的有效位数或小数位数。 例如,格式字符串 "{0:F2}" 显示具有两个小数位的定点数。

有关设置字符串格式的详细信息,请参阅格式设置类型

此属性的值存储在视图状态中。

适用于

另请参阅