Export (0) Print
Expand All

TreeNodeCollection Class

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

System.Object
  System.Web.UI.WebControls.TreeNodeCollection

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

public sealed class TreeNodeCollection : ICollection, 
	IEnumerable, IStateManager

The TreeNodeCollection type exposes the following members.

  NameDescription
Public methodTreeNodeCollection()Initializes a new instance of the TreeNodeCollection class using the default values.
Public methodTreeNodeCollection(TreeNode)Initializes a new instance of the TreeNodeCollection class using the specified parent node (or owner).
Top

  NameDescription
Public propertyCountGets the number of items in the TreeNodeCollection object.
Public propertyIsSynchronizedGets a value indicating whether access to the TreeNodeCollection is synchronized (thread safe).
Public propertyItemGets the TreeNode object at the specified index in the TreeNodeCollection object.
Public propertySyncRootGets an object that can be used to synchronize access to the TreeNodeCollection object.
Top

  NameDescription
Public methodAddAppends the specified TreeNode object to the end of the TreeNodeCollection object.
Public methodAddAtInserts the specified TreeNode object in a TreeNodeCollection object at the specified index location.
Public methodClearEmpties the TreeNodeCollection object.
Public methodContainsDetermines whether the specified TreeNode object is in the collection.
Public methodCopyToCopies all the items from the TreeNodeCollection object to a compatible one-dimensional array of TreeNode objects, starting at the specified index in the target array.
Public methodEquals(Object)Determines whether the specified object is equal to the current object. (Inherited from Object.)
Public methodGetEnumeratorReturns an enumerator that can be used to iterate through a TreeNodeCollection object.
Public methodGetHashCodeServes as the default hash function. (Inherited from Object.)
Public methodGetTypeGets the Type of the current instance. (Inherited from Object.)
Public methodIndexOfDetermines the index of the specified TreeNode object.
Public methodRemoveRemoves the specified TreeNode object from the TreeNodeCollection object.
Public methodRemoveAtRemoves the TreeNode object at the specified index location from the TreeNodeCollection object.
Public methodToStringReturns a string that represents the current object. (Inherited from Object.)
Top

  NameDescription
Public Extension MethodAsParallelEnables parallelization of a query. (Defined by ParallelEnumerable.)
Public Extension MethodAsQueryableConverts an IEnumerable to an IQueryable. (Defined by Queryable.)
Public Extension MethodCast<TResult>Casts the elements of an IEnumerable to the specified type. (Defined by Enumerable.)
Public Extension MethodOfType<TResult>Filters the elements of an IEnumerable based on a specified type. (Defined by Enumerable.)
Top

  NameDescription
Explicit interface implemetationPrivate methodICollection.CopyToInfrastructure. Copies all the items from the TreeNodeCollection object to a compatible one-dimensional System.Array, starting at the specified index in the target array.
Explicit interface implemetationPrivate propertyIStateManager.IsTrackingViewStateInfrastructure. Gets a value indicating whether the TreeNodeCollection object is saving changes to its view state.
Explicit interface implemetationPrivate methodIStateManager.LoadViewStateInfrastructure. Loads the TreeNodeCollection object's previously saved view state.
Explicit interface implemetationPrivate methodIStateManager.SaveViewStateInfrastructure. Saves the changes to view state to a System.Object.
Explicit interface implemetationPrivate methodIStateManager.TrackViewStateInfrastructure. Instructs the TreeNodeCollection to track changes to its view state.
Top

The TreeNodeCollection class is used to store and manage a collection of TreeNode objects in the TreeView control. The TreeView control uses the TreeNodeCollection class in two of its properties. It stores its root nodes in the Nodes property and its selected nodes in the CheckedNodes property. The TreeNodeCollection collection is also used for the ChildNodes property to store child nodes (if any).

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

  • Use the Item indexer to retrieve a TreeNode object directly at a specific zero-based index.

  • Use the GetEnumerator method to create an enumerator that can be used to iterate through the collection.

  • Use foreach (C#) or For Each (Visual Basic) to iterate through the collection.

  • Use the CopyTo method to copy the contents of the collection into an Array object.

You can programmatically manage the TreeNodeCollection by adding and removing TreeNode objects. To add nodes to the collection, use the Add or AddAt method. To remove nodes from the collection, use the Remove, RemoveAt, or Clear method.

NoteNote

When the TreeView control is bound to a data source, the Nodes and ChildNodes collections are automatically populated each time binding occurs. Any changes to the collections between bindings will be lost. To retain these changes, either update the data source or manually rebuild the collection each time you bind.

The TreeNodeCollection contains properties and methods that allow you to retrieve information about the collection itself. To find out how many items are in the collection, use the Count property. If you want to determine whether the collection contains a certain TreeNode object, use the Contains method. To get the index of a TreeNode object in the collection, use the IndexOf method.

The following example demonstrates how to programmatically add nodes to and remove them from a TreeNodeCollection. Notice that the Nodes and ChildNodes properties return a TreeNodeCollection object.


<%@ 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)
  {

    if (!IsPostBack)
    {

      // Use the Add and Remove methods to programmatically 
      // remove the Appendix C node and replace it with a new 
      // node. 
      LinksTreeView.Nodes.Remove(LinksTreeView.Nodes[3]);
      LinksTreeView.Nodes.Add(new TreeNode("New Appendix C"));

      // Use the AddAt and RemoveAt methods to programmatically 
      // remove the Chapter One node and replace it with a new node.
      LinksTreeView.Nodes[0].ChildNodes.RemoveAt(0);
      LinksTreeView.Nodes[0].ChildNodes.AddAt(0, new TreeNode("New Chapter One"));

      // Use the Clear method to remove all the child nodes of the 
      // Chapter Two node.
      LinksTreeView.Nodes[0].ChildNodes[1].ChildNodes.Clear();

    }

  }

</script>

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

      <h3>TreeNodeCollection Example</h3>

      <asp:TreeView id="LinksTreeView"
        Font-Names= "Arial"
        ForeColor="Blue"
        runat="server">

        <LevelStyles>

          <asp:TreeNodeStyle ChildNodesPadding="10" 
            Font-Bold="true" 
            Font-Size="12pt" 
            ForeColor="DarkGreen"/>
          <asp:TreeNodeStyle ChildNodesPadding="5" 
            Font-Bold="true" 
            Font-Size="10pt"/>
          <asp:TreeNodeStyle ChildNodesPadding="5" 
            Font-UnderLine="true" 
            Font-Size="10pt"/>
          <asp:TreeNodeStyle ChildNodesPadding="10" 
            Font-Size="8pt"/>

        </LevelStyles>

        <Nodes>

          <asp:TreeNode Text="Table of Contents"
            Expanded="true">

            <asp:TreeNode Text="Chapter One">

              <asp:TreeNode Text="Section 1.0">

                <asp:TreeNode Text="Topic 1.0.1"/>
                <asp:TreeNode Text="Topic 1.0.2"/>
                <asp:TreeNode Text="Topic 1.0.3"/>

              </asp:TreeNode>

              <asp:TreeNode Text="Section 1.1">

                <asp:TreeNode Text="Topic 1.1.1"/>
                <asp:TreeNode Text="Topic 1.1.2"/>
                <asp:TreeNode Text="Topic 1.1.3"/>
                <asp:TreeNode Text="Topic 1.1.4"/>

              </asp:TreeNode>

            </asp:TreeNode>

            <asp:TreeNode Text="Chapter Two">

              <asp:TreeNode Text="Section 2.0">

                <asp:TreeNode Text="Topic 2.0.1"/>
                <asp:TreeNode Text="Topic 2.0.2"/>

              </asp:TreeNode>

            </asp:TreeNode>

          </asp:TreeNode>

          <asp:TreeNode Text="Appendix A" />
          <asp:TreeNode Text="Appendix B" />
          <asp:TreeNode Text="Appendix C" />

        </Nodes>

      </asp:TreeView>

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

.NET Framework

Supported in: 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