HyperLinkField.DataNavigateUrlFormatString 属性

定义

获取或设置用于指定格式的字符串,HyperLinkField 对象中的超链接的 URL 将以此格式呈现。Gets or sets the string that specifies the format in which the URLs for the hyperlinks in a HyperLinkField object are rendered.

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

属性值

用于指定格式的字符串,HyperLinkField 中的超链接的 URL 将以此格式呈现。A string that specifies the format in which the URLs for the hyperlinks in a HyperLinkField are rendered. 默认值为空字符串 (""),表明没有对 URL 值应用特殊格式设置。The default is an empty string (""), which indicates that no special formatting is applied to the URL values.

示例

下面的代码示例演示如何使用 DataNavigateUrlFormatString 属性指定要应用于 HyperLinkField 对象中显示的超链接的导航 Url 的自定义格式。The following code example demonstrates how to use the DataNavigateUrlFormatString property to specify a custom format to apply to the navigation URLs of the hyperlinks displayed in a HyperLinkField object. DataTextField 属性指定的字段的值将作为查询字符串传递到在格式字符串中指定的网页中。The value of the field specified by the DataTextField property is passed as a query string to a Web page specified in the format string.


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

注解

使用 DataNavigateUrlFormatString 属性为 HyperLinkField 对象中的超链接的 Url 指定自定义格式。Use the DataNavigateUrlFormatString property to specify a custom format for the URLs of the hyperlinks in a HyperLinkField object. 如果未设置 DataNavigateUrlFormatString 属性,超链接的 Url 不会获得任何特殊格式。If the DataNavigateUrlFormatString property is not set, the hyperlinks' URLs do not get any special formatting.

备注

仅当设置了 DataNavigateUrlFields 属性时才应用格式字符串。The format string is applied only when the DataNavigateUrlFields 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 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.

占位符由两部分组成,用冒号分隔并括在大括号中,格式为 {ABxx}。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.

适用于

另请参阅