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

属性值

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全球化和本地化

适用于

另请参阅