TreeNodeCollection.GetEnumerator Method

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

Returns an enumerator that can be used to iterate through a TreeNodeCollection object.

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

public IEnumerator GetEnumerator ()
public final IEnumerator GetEnumerator ()
public final function GetEnumerator () : IEnumerator

Return Value

An enumerator that can be used to iterate through the TreeNodeCollection.

Use the GetEnumerator method to create an enumerator that can be easily iterated through to get each item in the TreeNodeCollection. To get the item currently pointed to in the enumerator, use the IEnumerator.Current property. Use the IEnumerator.MoveNext method to move to the next item. If you need to move the enumerator back to the beginning of the collection, use the IEnumerator.Reset method.


After you create an enumerator or use the IEnumerator.Reset method, you must call the IEnumerator.MoveNext method. Otherwise, the item represented by the IEnumerator.Current property is undefined.

As an alternative, you can also use the CopyTo method to create an System.Array object that can be used to access the items in the collection. Another alternative is to use a foreach or For Each structure to iterate through the collection.

The following example demonstrates how to use the GetEnumerator method to create an enumerator that contains root nodes of the TreeView control.

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

<script runat="server">

  void Page_Load(Object sender, EventArgs e)

    // If the TreeView control contains any root nodes, display the 
    // text value of each node. 
    if (LinksTreeView.Nodes.Count > 0)

      // Use the GetEnumerator method to create an enumerator 
      // that contains the root node data.
      IEnumerator nodeEnumerator = LinksTreeView.Nodes.GetEnumerator();

      // Iterate through the enumerator to display the root nodes.
      while (nodeEnumerator.MoveNext())

        Message.Text += ((TreeNode)(nodeEnumerator.Current)).Text + "<br>";



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




    <form runat="server">
      <h3>TreeNodeCollection GetEnumerator 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="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" />

      <asp:Label id="Message"


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