TreeNodeCollection Class

Note: This class is new in the .NET Framework version 2.0.

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

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

Public NotInheritable Class TreeNodeCollection
	Implements ICollection, IEnumerable, IStateManager
Dim instance As TreeNodeCollection

public final class TreeNodeCollection implements ICollection, IEnumerable, 
public final class TreeNodeCollection implements ICollection, IEnumerable, 

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.


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="VB" %>

<script runat="server">

  Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs)

    If Not IsPostBack Then

      ' Use the Add and Remove methods to programmatically 
      ' remove the Appendix C node and replace it with a new 
      ' node. 
      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.AddAt(0, New TreeNode("New Chapter One"))

      ' Use the Clear method to remove all the child nodes of the 
      ' Chapter Two node.

    End If

  End Sub


    <form runat="server">
      <h3>TreeNodeCollection Example</h3>
      <asp:TreeView id="LinksTreeView"
        Font-Name= "Arial"
          <asp:TreeNodeStyle ChildNodesPadding="10" 
          <asp:TreeNodeStyle ChildNodesPadding="5" 
          <asp:TreeNodeStyle ChildNodesPadding="5" 
          <asp:TreeNodeStyle ChildNodesPadding="10" 
          <asp:TreeNode Text="Table of Contents"
            <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 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 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 Text="Appendix A" />
          <asp:TreeNode Text="Appendix B" />
          <asp:TreeNode Text="Appendix C" />



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 98, Windows 2000 SP4, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition

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

.NET Framework

Supported in: 2.0