This documentation is archived and is not being maintained.

TreeView.Nodes Property

Gets a collection of TreeNode objects that represents the root nodes in the TreeView control.

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

public TreeNodeCollection Nodes { get; }

Property Value

Type: System.Web.UI.WebControls.TreeNodeCollection
A TreeNodeCollection that contains the root nodes in the TreeView.

Use the Nodes property to get a TreeNodeCollection object that contains all the root nodes in the tree. This collection is commonly used to quickly iterate through all the root nodes, or to access a specific root node in the tree.


A typical tree structure has only one root node; however, you can add multiple root nodes to the TreeView control.

The Nodes property can also be used to manage the root nodes in the tree programmatically. You can add, insert, remove, and retrieve TreeNode objects from the collection. Any updates to the collection are automatically reflected in the TreeView control after the next round trip to the server.

To access the child nodes of a root node, use the ChildNodes property of the node. Use the ChildNodes property of each subsequent parent-level node to navigate down the node levels.

The following code example demonstrates how to use the Nodes property to traverse the tree.

<%@ Page Language="C#" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" 
<script runat="server">

  void Page_Load(Object sender, EventArgs e)

    // If the TreeView control contains any root nodes, perform a 
    // preorder traversal of the tree and display the text of each node. 
    if(LinksTreeView.Nodes.Count > 0)

      // Iterate through the root nodes in the Nodes property. 
      for(int i=0; i<LinksTreeView.Nodes.Count; i++)

        // Display the nodes.



      Message.Text = "The TreeView control does not have any nodes.";



  void DisplayChildNodeText(TreeNode node)

    // Display the node's text value.
    Message.Text += node.Text + "<br />";

    // Iterate through the child nodes of the parent node passed into 
    // this method and display their values. 
    for(int i=0; i<node.ChildNodes.Count; i++)

      // Recursively call the DisplayChildNodeText method to 
      // traverse the tree and display all the child nodes.




<html xmlns="" >
  <head runat="server">
    <title>TreeNodeCollection Count Example</title>
    <form id="form1" runat="server">

      <h3>TreeNodeCollection Count Example</h3>

      <asp:TreeView id="LinksTreeView"
        Font-Names= "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="Subtopic 1"/>
                  <asp:TreeNode Text="Subtopic 2"/>

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




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



      <br /><br />

      <asp:Label id="Message"


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