HyperLinkField.DataTextFormatString 属性
定义
获取或设置用于指定格式的字符串,HyperLinkField 对象中的超链接标题将以此格式显示。Get or sets the string that specifies the format in which the hyperlink captions in a HyperLinkField object are displayed.
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
属性值
用于指定格式的字符串,HyperLinkField 中的超链接标题将以此格式显示。A string that specifies the format in which the hyperlink captions in a HyperLinkField are displayed. 默认值为空字符串 (""),表明没有对这些超链接标题应用特殊格式设置。The default is an empty string (""), which indicates that no special formatting is applied to the hyperlink captions.
示例
下面的代码示例演示如何使用 DataTextFormatString 属性设置绑定到对象中显示的超链接标题的值的格式 HyperLinkField 。The following code example demonstrates how to use the DataTextFormatString property to format the values bound to the hyperlink captions displayed in a HyperLinkField object. 值的格式设置为货币。The values are formatted as currency.
<%@ Page language="C#" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>HyperLinkField Example</title>
</head>
<body>
<form id="form1" runat="server">
<h3>HyperLinkField Example</h3>
<!-- Populate the Columns collection declaratively. -->
<!-- The UnitPrice field values are bound to the -->
<!-- captions of the hyperlinks in the HyperLinkField -->
<!-- field column, formatted as currency. The ProductID -->
<!-- field values are bound to the navigate URLs of the -->
<!-- hyperlinks. However, instead of being the actual -->
<!-- URL values, the product ID is passed to the linked -->
<!-- page as a parameter in the URL specified by the -->
<!-- DataNavigateUrlFormatString property. -->
<asp:gridview id="OrdersGridView"
datasourceid="OrdersSqlDataSource"
autogeneratecolumns="false"
runat="server">
<columns>
<asp:boundfield datafield="OrderID"
headertext="Order ID"/>
<asp:boundfield datafield="ProductID"
headertext="Product ID"/>
<asp:hyperlinkfield datatextfield="UnitPrice"
datatextformatstring="{0:c}"
datanavigateurlfields="ProductID"
datanavigateurlformatstring="~\details.aspx?ProductID={0}"
headertext="Price"
target="_blank" />
<asp:boundfield datafield="Quantity"
headertext="Quantity"/>
</columns>
</asp:gridview>
<!-- This example uses Microsoft SQL Server and connects -->
<!-- to the Northwind sample database. -->
<asp:sqldatasource id="OrdersSqlDataSource"
selectcommand="SELECT [OrderID], [ProductID], [UnitPrice], [Quantity] FROM [Order Details]"
connectionstring="server=localhost;database=northwind;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">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>HyperLinkField DataTextFormatString and DataNavigateUrlFormatString Example</title>
</head>
<body>
<form id="form1" runat="server">
<h3>HyperLinkField DataTextFormatString and DataNavigateUrlFormatString Example</h3>
<!-- Populate the Columns collection declaratively. -->
<!-- The UnitPrice field values are bound to the -->
<!-- captions of the hyperlinks in the HyperLinkField -->
<!-- field column, formatted as currency. The ProductID -->
<!-- field values are bound to the navigate URLs of the -->
<!-- hyperlinks. However, instead of being the actual -->
<!-- URL values, the product ID is passed to the linked -->
<!-- page as a parameter in the URL specified by the -->
<!-- DataNavigateUrlFormatString property. -->
<asp:gridview id="OrdersGridView"
datasourceid="OrdersSqlDataSource"
autogeneratecolumns="false"
runat="server">
<columns>
<asp:boundfield datafield="OrderID"
headertext="Order ID"/>
<asp:boundfield datafield="ProductID"
headertext="Product ID"/>
<asp:hyperlinkfield datatextfield="UnitPrice"
datatextformatstring="{0:c}"
datanavigateurlfields="ProductID"
datanavigateurlformatstring="~\details.aspx?ProductID={0}"
headertext="Price"
target="_blank" />
<asp:boundfield datafield="Quantity"
headertext="Quantity"/>
</columns>
</asp:gridview>
<!-- This example uses Microsoft SQL Server and connects -->
<!-- to the Northwind sample database. -->
<asp:sqldatasource id="OrdersSqlDataSource"
selectcommand="SELECT [OrderID], [ProductID], [UnitPrice], [Quantity] FROM [Order Details]"
connectionstring="server=localhost;database=northwind;integrated security=SSPI"
runat="server">
</asp:sqldatasource>
</form>
</body>
</html>
注解
使用 DataTextFormatString 属性为对象中显示的标题指定自定义的显示格式 HyperLinkField 。Use the DataTextFormatString property to specify a custom display format for the captions displayed in the HyperLinkField object. 如果 DataTextFormatString 未设置该属性,则将显示字段的值而不进行任何特殊格式设置。If the DataTextFormatString property is not set, the field's value is displayed without any special formatting.
备注
仅当设置了属性时,才应用格式字符串 DataTextField 。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 field's value. 例如,在格式字符串 "项目值:" 中 {0} , {0} 当字段显示在对象中时,占位符将被替换为该字段的值 HyperLinkField 。For example, in the format string "Item Value: {0}", the {0} placeholder is replaced with the field's value when it is displayed in the HyperLinkField object. 格式字符串的其余部分显示为文本文本。The rest of the format string is displayed as literal text.
备注
如果格式字符串不包含占位符,则不会在最终显示文本中包含来自数据源的字段值。If the format string does not include a placeholder, the field's value from the data source is not included in the final display text.
占位符由两部分组成,用冒号分隔并括在大括号中,格式为 { A : Bxx }。The placeholder consists of two parts, separated by a colon and wrapped in braces, in the form {A:Bxx}. 常规示例中冒号 (前面的值 A) 在参数的从零开始的列表中指定字段值的索引。The value before the colon (A in the general example) specifies the field value's index in a zero-based list of parameters.
备注
此参数是格式设置语法的一部分。This parameter is part of the formatting syntax. 由于每个单元格中只有一个字段值,因此此值只能设置为0。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. 常规示例中冒号后面的字符 (B) 指定显示值的格式。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. |
备注
格式字符不区分大小写,但除外 X ,后者在指定的情况下显示十六进制字符。The format characters are not case-sensitive, except for X, which displays the hexadecimal characters in the case specified.
常规示例中格式字符 (后面的值 xx) 指定要显示的有效位数或小数位数。The value after the format character (xx in the general example) specifies the number of significant digits or decimal places to display. 例如,格式字符串 " {0:F2} " 显示具有两个小数位的定点数。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.