TreeNodeBinding Class

Defines the relationship between a data item and the node it is binding to in a TreeView control.

Namespace:  System.Web.UI.WebControls
Assembly:  System.Web (in System.Web.dll)

[AspNetHostingPermissionAttribute(SecurityAction.LinkDemand, Level = AspNetHostingPermissionLevel.Minimal)]
public sealed class TreeNodeBinding : IStateManager, 
	ICloneable, IDataSourceViewSchemaAccessor

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.

NoteNote:

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.

Property

Description

ImageUrlField

The field to bind to the ImageUrl property of a TreeNode object.

ImageToolTipField

The field to bind to the ImageToolTip property of a TreeNode object.

NavigateUrlField

The field to bind to the NavigateUrl property of a TreeNode object.

TextField

The field to bind to the Text property of a TreeNode object.

ToolTipField

The field to bind to the ToolTip property of a TreeNode object.

ValueField

The field to bind to the Value property of a TreeNode object.

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.

Property

Description

ImageUrl

The static value to bind to the ImageUrl property of a TreeNode object.

ImageToolTip

The static value to bind to the ImageToolTip property of a TreeNode object.

NavigateUrl

The static value to bind to the NavigateUrl property of a TreeNode object.

PopulateOnDemand

The static value to bind to the PopulateOnDemand property of a TreeNode object.

SelectAction

The static value to bind to the SelectAction property of a TreeNode object.

ShowCheckBox

The static value to bind to the ShowCheckBox property of a TreeNode object.

Target

The static value to bind to the Target property of a TreeNode object.

Text

The static value to bind to the Text property of a TreeNode object.

ToolTip

The static value to bind to the ToolTip property of a TreeNode object.

Value

The static value to bind to the Value property of a TreeNode object.

If conflicting TreeNodeBinding objects are defined, the TreeView control applies the tree node bindings in the following order of precedence:

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

  2. The TreeNodeBinding object that defines and matches the data member only.

  3. The TreeNodeBinding object that defines and matches the depth only.

  4. 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.)

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

The following table shows some example tree node binding declarations.

Example binding

Description

<asp:TreeNodeBinding TextField="Title" ValueField= "ID"/>

Binds the Text and Value properties of all nodes in the tree to the Title and ID fields of the data source, respectively. All nodes use this tree node binding declaration because the DataMember and Depth properties are not set.

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

Binds the Text and Value properties of all nodes in the tree to the Title and ID fields of the Book data item in the data source, respectively.

<asp:TreeNodeBinding Depth="2" TextField= "Title" ValueField= "ID"/>

Binds the Text and Value properties of all nodes with a depth of 2 in the tree to the Title and ID fields of the data item in the data source, respectively.

<asp:TreeNodeBinding DataMember="Book" Depth= "2" TextField= "Title" ValueField= "ID" ImageUrl= "Image.jpg">

Binds the Text and Value properties of all nodes with a depth of 2 in the tree to the Title and ID fields of the Book data item in the data source, respectively. Also binds the ImageUrl property of the nodes to a static value.

This section contains three code examples. The first code example demonstrates how to use TreeNodeBinding objects declaratively to define the relationship between a node and a data item. The second code example demonstrates how to use TreeNodeBinding objects programmatically to define the relationship between a node and a data item. The third code example provides sample XML data for the first and second code examples.

The following code example demonstrates how to use TreeNodeBinding objects declaratively to define the relationship between a node and a data item. For this 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>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 demonstrates how to use TreeNodeBinding objects programmatically to define the relationship between a node and a data item. For this example to work correctly, you must copy the sample XML data that is provided in the next 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">
<script runat="server">

  void Page_Load(Object sender, EventArgs e)
  {

    // Create a new TreeView control.
    TreeView NewTree = new TreeView();

    // Set the properties of the TreeView control.
    NewTree.ID = "BookTreeView";
    NewTree.DataSourceID = "BookXmlDataSource";

    // Create the tree node binding relationship. 

    // Create the root node binding.
    TreeNodeBinding RootBinding = new TreeNodeBinding();
    RootBinding.DataMember = "Book";
    RootBinding.TextField = "Title";

    // Create the parent node binding.
    TreeNodeBinding ParentBinding = new TreeNodeBinding();
    ParentBinding.DataMember = "Chapter";
    ParentBinding.TextField = "Heading";

    // Create the leaf node binding.
    TreeNodeBinding LeafBinding = new TreeNodeBinding();
    LeafBinding.DataMember = "Section";
    LeafBinding.TextField = "Heading";

    // Add bindings to the DataBindings collection.
    NewTree.DataBindings.Add(RootBinding);
    NewTree.DataBindings.Add(ParentBinding); 
    NewTree.DataBindings.Add(LeafBinding);

    // Manually register the event handler for the SelectedNodeChanged event.
    NewTree.SelectedNodeChanged += new EventHandler(this.Node_Change);

    // Add the TreeView control to the Controls collection of the PlaceHolder control.
    ControlPlaceHolder.Controls.Add(NewTree);

  }

  void Node_Change(Object sender, EventArgs e)
  {

    // Retrieve the TreeView control from the Controls collection of the PlaceHolder control.
    TreeView LocalTree = (TreeView)ControlPlaceHolder.FindControl("BookTreeView");

    // Display the selected node.
    Message.Text = "You selected: " + LocalTree.SelectedNode.Text;

  }

</script>

<html xmlns="http://www.w3.org/1999/xhtml" >
  <head runat="server">
    <title>TreeView Constructor Example</title>
</head>
<body>
    <form id="form1" runat="server">

      <h3>TreeView Constructor Example</h3>

      <asp:PlaceHolder id="ControlPlaceHolder" runat="server">
      </asp:PlaceHolder>

      <asp:XmlDataSource id="BookXmlDataSource"  
        DataFile="Book.xml"
        runat="server">
      </asp:XmlDataSource>

      <br /><br />

      <asp:Label id="Message" runat="server"/>

    </form>
  </body>
</html>

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

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

System.Object
  System.Web.UI.WebControls.TreeNodeBinding

Any public static (Shared in Visual Basic) members of this type are thread safe. Any instance members are not guaranteed to be thread safe.

Windows 7, Windows Vista, Windows XP SP2, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP Starter Edition, Windows Server 2008 R2, Windows Server 2008, Windows Server 2003, Windows Server 2000 SP4, Windows Millennium Edition, Windows 98

The .NET Framework and .NET Compact Framework do not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.

.NET Framework

Supported in: 3.5, 3.0, 2.0
Was this page helpful?
(1500 characters remaining)
Thank you for your feedback

Community Additions

ADD
Show:
© 2014 Microsoft