TreeNodeBinding.FormatString 屬性

定義

取得或設定字串,指定套用 TreeNodeBinding 物件之節點文字的顯示格式。

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

屬性值

格式化字串,指定套用 TreeNodeBinding 物件之節點文字的顯示格式。 預設值是空字串 (""),表示未設定 FormatString 屬性。

範例

本節包含兩個程式碼範例。 第一個程式碼範例示範如何使用 FormatString 屬性,為根節點中顯示的文字指定自訂顯示格式。 第二個程式碼範例會針對第一個程式碼範例提供範例 XML 資料。

下列程式碼範例示範如何使用 FormatString 屬性,為根節點中顯示的文字指定自訂顯示格式。 若要讓此範例正常運作,您必須將此程式碼範例之後提供的範例 XML 資料複製到名為 Book.xml 的檔案。


<%@ 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>TreeNodeBinding FormatString Example</title>
</head>
<body>
    <form id="form1" runat="server">
    
      <h3>TreeNodeBinding FormatString Example</h3>
    
      <!-- Use the FormatString property to apply   -->
      <!-- a custom format string to the root node. -->
      <!-- The placeholder ({0}) is automatically   -->
      <!-- replaced with the value of the field     -->
      <!-- specified in the TextField property.     -->
      <asp:TreeView id="BookTreeView" 
         DataSourceID="BookXmlDataSource"
         runat="server">
          
        <DataBindings>
          <asp:TreeNodeBinding DataMember="Book" 
            Depth="0"
            TextField="Title" 
            FormatString="Best Seller: {0}"/>
          <asp:TreeNodeBinding DataMember="Chapter" 
            Depth="1" 
            TextField="Heading"/>
          <asp:TreeNodeBinding DataMember="Appendix" 
            Depth="1" 
            TextField="Heading"/>
        </DataBindings>
         
      </asp:TreeView>
      
      <asp:XmlDataSource id="BookXmlDataSource"  
         DataFile="Book.xml"
         runat="server">
      </asp:XmlDataSource>
    
    </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>TreeNodeBinding FormatString Example</title>
</head>
<body>
    <form id="form1" runat="server">
    
      <h3>TreeNodeBinding FormatString Example</h3>
    
      <!-- Use the FormatString property to apply   -->
      <!-- a custom format string to the root node. -->
      <!-- The placeholder ({0}) is automatically   -->
      <!-- replaced with the value of the field     -->
      <!-- specified in the TextField property.     --> 
      <asp:TreeView id="BookTreeView" 
         DataSourceID="BookXmlDataSource"
         runat="server">
          
        <DataBindings>
          <asp:TreeNodeBinding DataMember="Book" 
            Depth="0"
            TextField="Title" 
            FormatString="Best Seller: {0}"/>
          <asp:TreeNodeBinding DataMember="Chapter" 
            Depth="1" 
            TextField="Heading"/>
          <asp:TreeNodeBinding DataMember="Appendix" 
            Depth="1" 
            TextField="Heading"/>
        </DataBindings>
         
      </asp:TreeView>
      
      <asp:XmlDataSource id="BookXmlDataSource"  
         DataFile="Book.xml"
         runat="server">
      </asp:XmlDataSource>
    
    </form>
  </body>
</html>

下列程式碼範例是上述範例的 XML 資料範例。

<Book Title="Book Title">  
    <Chapter Heading="Chapter 1">  
        <Section Heading="Section 1">  
        </Section>  
        <Section Heading="Section 2">  
        </Section>  
    </Chapter>  
    <Chapter Heading="Chapter 2">  
        <Section Heading="Section 1">  
        </Section>  
    </Chapter>  
    <Appendix Heading="Appendix A">  
    </Appendix>  
</Book>  

備註

屬性 FormatString 是用來提供節點文字的自訂格式。 資料格式字串是由兩個部分所組成,並以冒號分隔,格式為 { ABxx }。 例如,格式化字串 {0:F2} 會顯示具有兩個小數位數的固定點數。

注意

整個字串必須以大括弧括住,以指出它是格式字串,而不是常值字串。 大括弧以外的任何文字都會顯示為常值文字。

一般範例中的冒號 (A 之前的值,) 在以零起始的參數清單中指定參數索引。

注意

此值只能設定為 0,因為每個節點中只有一個值。

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

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

注意

X除了 之外,格式字元不會區分大小寫。 格式 X 字元會在指定的情況下顯示十六進位字元。

在一般範例 (xx 格式字元之後的值,) 會指定要顯示的有效位數或小數位數。

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

此屬性的值會儲存在檢視狀態中。

設定時,屬性的值 FormatString 可以使用設計工具自動儲存到資源檔。 如需詳細資訊,請參閱 LocalizableAttribute和全球化和當地語系化

適用於

另請參閱