HyperLinkField.DataNavigateUrlFormatString 屬性

定義

取得或設定字串,指定 HyperLinkField 物件中超連結之 URL 的呈現格式。

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 的呈現格式。 預設為空字串 (""),表示 URL 值未套用任何特殊格式。

範例

下列程式碼範例示範如何使用 DataNavigateUrlFormatString 屬性來指定自訂格式,以套用至物件中所 HyperLinkField 顯示超連結的導覽 URL。 屬性所 DataTextField 指定的域值會當做查詢字串傳遞至格式字串中指定的網頁。


<%@ 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 的自訂格式。 DataNavigateUrlFormatString如果未設定屬性,超連結的 URL 不會取得任何特殊格式設定。

注意

只有在設定 屬性時, DataNavigateUrlFields 才會套用格式字串。

格式字串可以是任何常值字串,通常包含域值的預留位置。 例如,在格式字串 「Item Value: {0} 」 中 {0} ,預留位置會取代為 物件中的 HyperLinkField 域值。 格式字串的其餘部分會顯示為常值文字。

注意

如果格式字串不包含預留位置,則資料來源中的欄位值不會包含在最終顯示文字中。

預留位置包含兩個部分,以冒號分隔,並以大括弧括住,格式為 { ABxx }。 在一般範例中,冒號前的值 (A) 在以零起始的參數清單中指定域值的索引。

注意

此參數是格式化語法的一部分。 因為每個儲存格中只有一個域值,所以這個值只能設定為 0。

冒號和冒號之後的值是選擇性的。 在一般範例中,冒號之後的字元 (B) 會指定要顯示值的格式。 下表列出常見的格式。

格式化字元 說明
C 以貨幣格式顯示數值。
D 以十進位格式顯示數值。
E 以科學 (指數) 格式顯示數值。
F 以固定格式顯示數值。
G 以一般格式顯示數值。
N 以數位格式顯示數值。
X 以十六進位格式顯示數值。

注意

格式字元不區分大小寫,但 除外 X ,它會在指定的大小寫中顯示十六進位字元。

在一般範例中,格式字元 (xx 後的值) 會指定要顯示的有效位數或小數位數。 例如,格式字串 「 {0:F2} 」 會顯示具有兩個小數位數的固定點數。

如需格式化字串的詳細資訊,請參閱格式化類型

適用於

另請參閱