Bearbeiten

Freigeben über


TreeNodeBinding.DataMember Property

Definition

Gets or sets the value to match against a Type property for a data item to determine whether to apply the tree node binding.

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

Property Value

The value to match against a data item's Type property to determine whether to apply the tree node binding. The default is an empty string (""), which indicates that the DataMember property is not set.

Examples

This section contains two code examples. The first code example demonstrates how to use the DataMember property to specify which XML element to bind to a node. The second code example provides sample XML data for the first code example.

The following code example demonstrates how to use the DataMember property to specify which XML element to bind to a node. For this code example to work correctly, you must copy the sample XML data, provided after this code example, to a file named 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>

The following code example provides sample XML data for the preceding code example.

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

Remarks

A data member specifies the type of the data item in the underlying data source, but can represent different information depending on the data source. Each data item in a hierarchical data source (represented by a System.Web.UI.IHierarchyData object) exposes a IHierarchyData.Type property, which specifies the type of the data item. For example, the data member for an XML element specifies the name of the element. When a data source contains multiple data item types, the data member specifies which data item type to use. The following TreeNodeBinding declaration binds the <Book> elements of an XmlDataSource control to all the nodes in the tree, regardless of the location in the hierarchy:

<asp:TreeNodeBinding DataMember="Book" TextField="Title" ValueField= "ISBN">  

When creating a TreeNodeBinding object, you must specify the criteria for binding. The criteria indicates when a data item should be bound to a node. You can specify the Depth or DataMember property, or both properties. There is a slight performance gain by specifying both.

Once the binding criteria is established, you can then bind a property of a TreeNode object that can be bound to a value. You can bind to a field of a data item or to a static value. When bound to a static value, all TreeNode objects to which the TreeNodeBinding object is applied share the same value.

The value of this property is stored in view state.

Applies to

See also