TreeNodeBindingCollection Class
TOC
Collapse the table of content
Expand the table of content

TreeNodeBindingCollection Class

 

Represents a collection of TreeNodeBinding objects in the TreeView control. This class cannot be inherited.

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

System.Object
  System.Web.UI.StateManagedCollection
    System.Web.UI.WebControls.TreeNodeBindingCollection

public sealed class TreeNodeBindingCollection : StateManagedCollection

NameDescription
System_CAPS_pubpropertyCount

Gets the number of elements contained in the StateManagedCollection collection.(Inherited from StateManagedCollection.)

System_CAPS_pubpropertyItem[Int32]

Gets or sets the TreeNodeBinding object at the specified index in the TreeNodeBindingCollection object.

NameDescription
System_CAPS_pubmethodAdd(TreeNodeBinding)

Appends the specified TreeNodeBinding object to the end of the TreeNodeBindingCollection object.

System_CAPS_pubmethodClear()

Removes all items from the StateManagedCollection collection.(Inherited from StateManagedCollection.)

System_CAPS_pubmethodContains(TreeNodeBinding)

Determines whether the specified TreeNodeBinding object is in the collection.

System_CAPS_pubmethodCopyTo(Array, Int32)

Copies the elements of the StateManagedCollection collection to an array, starting at a particular array index.(Inherited from StateManagedCollection.)

System_CAPS_pubmethodCopyTo(TreeNodeBinding[], Int32)

Copies all the items from the TreeNodeBindingCollection object to a compatible one-dimensional array of TreeNodeBinding objects, starting at the specified index in the target array.

System_CAPS_pubmethodEquals(Object)

Determines whether the specified object is equal to the current object.(Inherited from Object.)

System_CAPS_pubmethodGetEnumerator()

Returns an iterator that iterates through the StateManagedCollection collection.(Inherited from StateManagedCollection.)

System_CAPS_pubmethodGetHashCode()

Serves as the default hash function. (Inherited from Object.)

System_CAPS_pubmethodGetType()

Gets the Type of the current instance.(Inherited from Object.)

System_CAPS_pubmethodIndexOf(TreeNodeBinding)

Determines the index of the specified TreeNodeBinding object in the collection.

System_CAPS_pubmethodInsert(Int32, TreeNodeBinding)

Inserts the specified TreeNodeBinding object into the TreeNodeBindingCollection object at the specified index location.

System_CAPS_pubmethodRemove(TreeNodeBinding)

Removes the specified TreeNodeBinding object from the TreeNodeBindingCollection object.

System_CAPS_pubmethodRemoveAt(Int32)

Removes the TreeNodeBinding object at the specified index location from the TreeNodeBindingCollection object.

System_CAPS_pubmethodSetDirty()

Forces the entire StateManagedCollection collection to be serialized into view state. (Inherited from StateManagedCollection.)

System_CAPS_pubmethodToString()

Returns a string that represents the current object.(Inherited from Object.)

NameDescription
System_CAPS_pubinterfaceSystem_CAPS_privmethodIEnumerable.GetEnumerator()

This API supports the product infrastructure and is not intended to be used directly from your code. Returns an iterator that iterates through the StateManagedCollection collection.(Inherited from StateManagedCollection.)

System_CAPS_pubinterfaceSystem_CAPS_privmethodIList.Add(Object)

Adds an item to the StateManagedCollection collection.(Inherited from StateManagedCollection.)

System_CAPS_pubinterfaceSystem_CAPS_privmethodIList.Clear()

This API supports the product infrastructure and is not intended to be used directly from your code. Removes all items from the StateManagedCollection collection.(Inherited from StateManagedCollection.)

System_CAPS_pubinterfaceSystem_CAPS_privmethodIList.Contains(Object)

Determines whether the StateManagedCollection collection contains a specific value.(Inherited from StateManagedCollection.)

System_CAPS_pubinterfaceSystem_CAPS_privmethodIList.IndexOf(Object)

Determines the index of a specified item in the StateManagedCollection collection.(Inherited from StateManagedCollection.)

System_CAPS_pubinterfaceSystem_CAPS_privmethodIList.Insert(Int32, Object)

Inserts an item into the StateManagedCollection collection at the specified index.(Inherited from StateManagedCollection.)

System_CAPS_pubinterfaceSystem_CAPS_privmethodIList.Remove(Object)

Removes the first occurrence of the specified object from the StateManagedCollection collection.(Inherited from StateManagedCollection.)

System_CAPS_pubinterfaceSystem_CAPS_privmethodIList.RemoveAt(Int32)

Removes the IStateManager element at the specified index.(Inherited from StateManagedCollection.)

System_CAPS_pubinterfaceSystem_CAPS_privmethodIStateManager.LoadViewState(Object)

Restores the previously saved view state of the StateManagedCollection collection and the IStateManager items it contains.(Inherited from StateManagedCollection.)

System_CAPS_pubinterfaceSystem_CAPS_privmethodIStateManager.SaveViewState()

Saves the changes to the StateManagedCollection collection and each IStateManager object it contains since the time the page was posted back to the server.(Inherited from StateManagedCollection.)

System_CAPS_pubinterfaceSystem_CAPS_privmethodIStateManager.TrackViewState()

Causes the StateManagedCollection collection and each of the IStateManager objects it contains to track changes to their view state so they can be persisted across requests for the same page.(Inherited from StateManagedCollection.)

System_CAPS_pubinterfaceSystem_CAPS_privpropertyICollection.Count

This API supports the product infrastructure and is not intended to be used directly from your code. Gets the number of elements contained in the StateManagedCollection collection.(Inherited from StateManagedCollection.)

System_CAPS_pubinterfaceSystem_CAPS_privpropertyICollection.IsSynchronized

This API supports the product infrastructure and is not intended to be used directly from your code. Gets a value indicating whether the StateManagedCollection collection is synchronized (thread safe). This method returns false in all cases.(Inherited from StateManagedCollection.)

System_CAPS_pubinterfaceSystem_CAPS_privpropertyICollection.SyncRoot

This API supports the product infrastructure and is not intended to be used directly from your code. Gets an object that can be used to synchronize access to the StateManagedCollection collection. This method returns null in all cases.(Inherited from StateManagedCollection.)

System_CAPS_pubinterfaceSystem_CAPS_privpropertyIList.IsFixedSize

This API supports the product infrastructure and is not intended to be used directly from your code. Gets a value indicating whether the StateManagedCollection collection has a fixed size. This method returns false in all cases.(Inherited from StateManagedCollection.)

System_CAPS_pubinterfaceSystem_CAPS_privpropertyIList.IsReadOnly

This API supports the product infrastructure and is not intended to be used directly from your code. Gets a value indicating whether the StateManagedCollection collection is read-only.(Inherited from StateManagedCollection.)

System_CAPS_pubinterfaceSystem_CAPS_privpropertyIList.Item[Int32]

This API supports the product infrastructure and is not intended to be used directly from your code. Gets the IStateManager element at the specified index.(Inherited from StateManagedCollection.)

System_CAPS_pubinterfaceSystem_CAPS_privpropertyIStateManager.IsTrackingViewState

Gets a value indicating whether the StateManagedCollection collection is saving changes to its view state.(Inherited from StateManagedCollection.)

NameDescription
System_CAPS_pubmethodAsParallel()

Overloaded. Enables parallelization of a query.(Defined by ParallelEnumerable.)

System_CAPS_pubmethodAsQueryable()

Overloaded. Converts an IEnumerable to an IQueryable.(Defined by Queryable.)

System_CAPS_pubmethodCast<TResult>()

Casts the elements of an IEnumerable to the specified type.(Defined by Enumerable.)

System_CAPS_pubmethodOfType<TResult>()

Filters the elements of an IEnumerable based on a specified type.(Defined by Enumerable.)

The TreeNodeBindingCollection class is used to store and manage a collection of TreeNodeBinding objects in the TreeView control. The TreeView control uses the TreeNodeBindingCollection class for its DataBindings property.

The DataBindings property 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. Although the DataBindings collection can be programmatically populated, it is usually set declaratively.

To set the tree node bindings declaratively:

  1. Nest opening and closing <DataBindings> tags between the opening and closing tags of the TreeView control.

  2. Place <asp:TreeNodeBinding> elements between the opening and closing <DataBindings> tags for each tree node binding that you want to specify.

You can programmatically manage a TreeNodeBindingCollection by adding and removing TreeNodeBinding objects. To add a TreeNodeBinding object to the collection, use the Add or Insert method. To remove nodes from the collection, use the Remove, RemoveAt, or StateManagedCollection.Clear method.

The TreeNodeBindingCollection class supports several ways to access the items in the collection:

This section contains two code examples. The first code example demonstrates how to populate a TreeNodeBindingCollection object declaratively. The second code example demonstrates how to populate a TreeNodeBindingCollection object programmatically.

The following code example demonstrates how to populate a TreeNodeBindingCollection object declaratively. For this example to work correctly, you must copy the XML data that is located at the end of this section to a file called 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 populate a TreeNodeBindingCollection object programmatically. For this example to work correctly, you must copy the sample XML data that is located at the end of this section to a file called 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 is the 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
Available since 2.0

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

Return to top
Show:
© 2016 Microsoft