Export (0) Print
Expand All

TreeNodeBinding Class

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

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

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

public sealed class TreeNodeBinding : IStateManager, 
	ICloneable, IDataSourceViewSchemaAccessor

The TreeNodeBinding type exposes the following members.

  NameDescription
Public methodTreeNodeBindingInitializes a new instance of the TreeNodeBinding class.
Top

  NameDescription
Public propertyDataMemberGets or sets the value to match against a IHierarchyData.Type property for a data item to determine whether to apply the tree node binding.
Public propertyDepthGets or sets the node depth at which the TreeNodeBinding object is applied.
Public propertyFormatStringGets or sets the string that specifies the display format for the text of a node to which the TreeNodeBinding object is applied.
Public propertyImageToolTipGets or sets the ToolTip text for the image that is displayed next to a node to which the TreeNodeBinding object is applied.
Public propertyImageToolTipFieldGets or sets the name of the field from the data source to bind to the ImageToolTip property of a TreeNode object to which the TreeNodeBinding object is applied.
Public propertyImageUrlGets or sets the URL to an image that is displayed next to a node to which the TreeNodeBinding object is applied.
Public propertyImageUrlFieldGets or sets the name of the field from the data source to bind to the ImageUrl property of a TreeNode object to which the TreeNodeBinding object is applied.
Public propertyNavigateUrlGets or sets the URL to link to when a node to which the TreeNodeBinding object is applied is clicked.
Public propertyNavigateUrlFieldGets or sets the name of the field from the data source to bind to the NavigateUrl property of a TreeNode object to which the TreeNodeBinding object is applied.
Public propertyPopulateOnDemandGets or sets a value indicating whether the node to which the TreeNodeBinding object is applied is populated dynamically.
Public propertySelectActionGets or sets the event or events to raise when a node to which the TreeNodeBinding object is applied is selected.
Public propertyShowCheckBoxGets or sets a value indicating whether a check box is displayed next to a node to which the TreeNodeBinding object is applied.
Public propertyTargetGets or sets the target window or frame in which to display the Web page content that is associated with a node to which the TreeNodeBinding object is applied.
Public propertyTargetFieldGets or sets the name of the field from the data source to bind to the Target property of a TreeNode object to which the TreeNodeBinding object is applied.
Public propertyTextGets or sets the text that is displayed for the node to which the TreeNodeBinding object is applied.
Public propertyTextFieldGets or sets the name of the field from the data source to bind to the Text property of a TreeNode object to which the TreeNodeBinding object is applied.
Public propertyToolTipGets or sets the ToolTip text for a node to which the TreeNodeBinding object is applied.
Public propertyToolTipFieldGets or sets the name of the field from the data source to bind to the ToolTip property of a TreeNode object to which the TreeNodeBinding object is applied.
Public propertyValueGets or sets a displayed value that is not displayed but is used to store any additional data about a node to which the TreeNodeBinding object is applied, such as data used for handling postback events.
Public propertyValueFieldGets or sets the name of the field from the data source to bind to the Value property of a TreeNode object to which the TreeNodeBinding object is applied.
Top

  NameDescription
Public methodEquals(Object)Determines whether the specified object is equal to the current object. (Inherited from Object.)
Public methodGetHashCodeServes as the default hash function. (Inherited from Object.)
Public methodGetTypeGets the Type of the current instance. (Inherited from Object.)
Public methodToStringReturns the DataMember property. (Overrides Object.ToString().)
Top

  NameDescription
Explicit interface implemetationPrivate methodICloneable.CloneInfrastructure. Creates a copy of the TreeNodeBinding object.
Explicit interface implemetationPrivate propertyIDataSourceViewSchemaAccessor.DataSourceViewSchemaInfrastructure. For a description of this member, see IDataSourceViewSchemaAccessor.DataSourceViewSchema.
Explicit interface implemetationPrivate propertyIStateManager.IsTrackingViewStateInfrastructure. For a description of this member, see IStateManager.IsTrackingViewState.
Explicit interface implemetationPrivate methodIStateManager.LoadViewStateInfrastructure. Loads the previously saved view state for the node.
Explicit interface implemetationPrivate methodIStateManager.SaveViewStateInfrastructure. Saves the view state changes to an object.
Explicit interface implemetationPrivate methodIStateManager.TrackViewStateInfrastructure. Instructs the TreeNode object to track changes to its view state.
Top

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>

.NET Framework

Supported in: 4.6, 4.5, 4, 3.5, 3.0, 2.0

Windows 8.1, Windows Server 2012 R2, Windows 8, Windows Server 2012, Windows 7, Windows Vista SP2, Windows Server 2008 (Server Core Role not supported), Windows Server 2008 R2 (Server Core Role supported with SP1 or later; Itanium not supported)

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

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