When the TreeView control is bound to a data source where each data item contains multiple fields (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 the properties of a node to a specific field by specifying tree node bindings. A TreeNodeBinding object defines the relationship between each data item and the node that it is binding to.
The TreeView control stores its TreeNodeBinding objects in the DataBindings property and applies the bindings to the data source to create a one-to-one relationship between the tree hierarchy and the data source hierarchy. For each data item in the data source, the TreeView control attempts to match the data item to a TreeNodeBinding object in order to create the corresponding TreeNode object.
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. A node depth specifies the node level that gets bound. For example, the following TreeNodeBinding declaration binds the Name and ID fields of the data source to the Text and Value properties, respectively, of all nodes with a depth of 0:
<asp:TreeNodeBinding Depth="0" TextField="Name" ValueField="ID">
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 interface) 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">
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.
Note: |
|---|
You can selectively override a bound property in a
TreeNode object by setting the corresponding property directly in the node.
|
The following table lists the properties of the TreeNodeBinding class that allow you to bind a property of the TreeNode object to a field of a data item.
The following table lists the properties of the TreeNodeBinding class that allow you to bind a property of the TreeNode object to a static value.
If conflicting TreeNodeBinding objects are defined, the TreeView control applies the tree node bindings in the following order of precedence:
The TreeNodeBinding object that defines and matches both a depth and a data member.
The TreeNodeBinding object that defines and matches the data member only.
The TreeNodeBinding object that defines and matches the depth only.
The TreeNodeBinding object that defines neither the depth nor the data member. (This type of tree node binding is applied to all nodes in the tree.)
The TreeNodeBinding object that does not have a match in the data source. In this case, the value that is returned by the ToString method of the data item is then bound to the Text and Value properties of the nodes to which the TreeNodeBinding object is applied.
The TreeNodeBinding class also allows you to format the text that is displayed in a node by setting the FormatString property.