Bewerken

Share via


TreeView.DataBindings Property

Definition

Gets a collection of TreeNodeBinding objects that define the relationship between a data item and the node that it is binding to.

public:
 property System::Web::UI::WebControls::TreeNodeBindingCollection ^ DataBindings { System::Web::UI::WebControls::TreeNodeBindingCollection ^ get(); };
[System.Web.UI.PersistenceMode(System.Web.UI.PersistenceMode.InnerProperty)]
public System.Web.UI.WebControls.TreeNodeBindingCollection DataBindings { get; }
[<System.Web.UI.PersistenceMode(System.Web.UI.PersistenceMode.InnerProperty)>]
member this.DataBindings : System.Web.UI.WebControls.TreeNodeBindingCollection
Public ReadOnly Property DataBindings As TreeNodeBindingCollection

Property Value

A TreeNodeBindingCollection that represents the relationship between a data item and the node that it is binding to.

Attributes

Examples

This section contains two code examples. The first code example demonstrates how to use the DataBindings collection to define the relationship between a data item and the node that it is binding to. The second code example provides sample XML data for the first code example.

The following code example demonstrates how to use the DataBindings collection to define the relationship between a data item and the node that it is binding to. For this example to work correctly, you must copy the sample XML data, provided after this code example, to a file named Book.xml. This example specifies only the DataMember property of the binding. A slight performance gain could be added by also specifying the Depth property.


<%@ 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>TreeView XML Data Binding Example</title>
</head>
<body>
    <form id="form1" runat="server">
    
      <h3>TreeView XML Data Binding Example</h3>
    
      <asp:TreeView id="BookTreeView" 
        DataSourceID="BookXmlDataSource"
        runat="server">
         
        <DataBindings>
          <asp:TreeNodeBinding DataMember="Book" TextField="Title"/>
          <asp:TreeNodeBinding DataMember="Chapter" TextField="Heading"/>
          <asp:TreeNodeBinding DataMember="Section" 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>TreeView XML Data Binding Example</title>
</head>
<body>
    <form id="form1" runat="server">
    
      <h3>TreeView XML Data Binding Example</h3>
    
      <asp:TreeView id="BookTreeView" 
        DataSourceID="BookXmlDataSource"
        runat="server">
         
        <DataBindings>
          <asp:TreeNodeBinding DataMember="Book" TextField="Title"/>
          <asp:TreeNodeBinding DataMember="Chapter" TextField="Heading"/>
          <asp:TreeNodeBinding DataMember="Section" 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 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>
</Book>

Remarks

The DataBindings collection contains TreeNodeBinding objects that define the relationship between a data item and the node that it is binding to. When binding to a data source where each data item contains multiple properties (such as an XML element with several attributes), a node displays the value that is returned by the ToString method of the data item, by default. In the case of an XML element, the node displays the element name, which shows the underlying structure of the tree, but is not very useful otherwise. You can bind a node to a specific data item property by specifying tree node bindings.

When defining the relationship between a data item and a node, you must specify both the criteria for binding and the values to bind to the properties of TreeNode object. The criteria indicate when a data item should be bound to a node. The criteria can be specified with a node depth, a data member, or both. A node depth specifies the node level that gets bound. For example, if you specify a node depth of 0, all nodes in the tree structure at level 0 are bound using the tree node binding. 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. For example, the data member for an XML element specifies the name of the element.

If multiple TreeNodeBinding objects are defined that conflict with each other, the TreeView control applies the tree node bindings in the following order of precedence:

  1. The TreeNodeBinding object that defines both a depth and a data member.

  2. The TreeNodeBinding object that defines only the depth.

  3. The TreeNodeBinding object that defines only the data member.

  4. The TreeNodeBinding object that defines neither the depth nor the data member.

If multiple bindings are specified that meet the same precedence criteria, the first binding in the collection is applied.

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 either bind to an attribute or field of a data item or display a static value. For more information on binding the properties of a TreeNode object to a value, see TreeNodeBinding.

Although the DataBindings collection can be programmatically populated, it is usually set declaratively. To specify the tree node bindings, first nest opening and closing <DataBindings> tags between the opening and closing tags of the TreeView control. Next, place <asp:TreeNodeBinding> elements between the opening and closing <DataBindings> tags for each tree node binding you want to specify.

When data bindings are created by setting the AutoGenerateDataBindings property of the TreeView control to true, the bindings that are created have the PopulateOnDemand property set to true. Data bindings that are created declaratively have the PopulateOnDemand property set to false. Using the declarative syntax allows you to control the behavior of individual data bindings.

Applies to

See also