TreeNodeBinding.DataMember 属性
定义
重要
一些信息与预发行产品相关,相应产品在发行之前可能会进行重大修改。 对于此处提供的信息,Microsoft 不作任何明示或暗示的担保。
获取或设置要与 Type 数据项的属性匹配的值,以确定是否应用树节点绑定。
public:
property System::String ^ DataMember { System::String ^ get(); void set(System::String ^ value); };
public string DataMember { get; set; }
member this.DataMember : string with get, set
Public Property DataMember As String
属性值
要与数据项 Type 的属性匹配的值,以确定是否应用树节点绑定。 默认值为空字符串(“),指示 DataMember 未设置属性。
示例
本部分包含两个代码示例。 第一个代码示例演示如何使用 DataMember 属性指定要绑定到节点的 XML 元素。 第二个代码示例提供第一个代码示例的示例 XML 数据。
下面的代码示例演示如何使用 DataMember 属性指定要绑定到节点的 XML 元素。 要使此代码示例正常工作,必须将在此代码示例后面提供的示例 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>TreeViewBinding DataMember and Depth Example</title>
</head>
<body>
<form id="form1" runat="server">
<h3>TreeViewBinding DataMember and Depth Example</h3>
<!-- Set the DataMember and Depth properties of a -->
<!-- TreeNodeBinding object declaratively. You -->
<!-- can render items at the same node level -->
<!-- by setting each item's Depth property to -->
<!-- the same value. -->
<asp:TreeView id="BookTreeView"
DataSourceID="BookXmlDataSource"
runat="server">
<DataBindings>
<asp:TreeNodeBinding DataMember="Book" Depth="0" TextField="Title"/>
<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>TreeViewBinding DataMember and Depth Example</title>
</head>
<body>
<form id="form1" runat="server">
<h3>TreeViewBinding DataMember and Depth Example</h3>
<!-- Set the DataMember and Depth properties of a -->
<!-- TreeNodeBinding object declaratively. You -->
<!-- can render items at the same node level -->
<!-- by setting each item's Depth property to -->
<!-- the same value. -->
<asp:TreeView id="BookTreeView"
DataSourceID="BookXmlDataSource"
runat="server">
<DataBindings>
<asp:TreeNodeBinding DataMember="Book" Depth="0" TextField="Title"/>
<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>
注解
数据成员指定基础数据源中的数据项的类型,但可以根据数据源表示不同的信息。 分层数据源(由 System.Web.UI.IHierarchyData 对象表示)中的每个数据项都会公开一个 IHierarchyData.Type 属性,该属性指定数据项的类型。 例如,XML 元素的数据成员指定元素的名称。 当数据源包含多个数据项类型时,数据成员指定要使用的数据项类型。 以下 TreeNodeBinding 声明将 <Book> 控件的 XmlDataSource 元素绑定到树中的所有节点,而不考虑层次结构中的位置:
<asp:TreeNodeBinding DataMember="Book" TextField="Title" ValueField= "ISBN">
创建 TreeNodeBinding 对象时,必须指定绑定的条件。 条件指示何时应将数据项绑定到节点。 可以指定或DataMember属性,也可以同时指定Depth这两个属性。 通过指定这两者,性能略有提高。
建立绑定条件后,可以绑定可绑定到值的对象的属性 TreeNode 。 可以绑定到数据项的字段或静态值。 绑定到静态值时,应用该对象的所有 TreeNode 对象 TreeNodeBinding 共享相同的值。
此属性的值存储在视图状态中。