Export (0) Print
Expand All

TreeNodeCollection.GetEnumerator Method

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()

Return Value

Type: System.Collections.IEnumerator
An enumerator that can be used to iterate through the TreeNodeCollection.

Implements

IEnumerable.GetEnumerator()

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.

NoteNote:

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#" %>

<!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 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 />";

      }

    }
    else
    {

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

    }

  }

</script>

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

      <h3>TreeNodeCollection GetEnumerator 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="Subtopic 1"/>
                  <asp:TreeNode Text="Subtopic 2"/>

                </asp:TreeNode>
                <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>

      <br /><br />

      <asp:Label id="Message"
        runat="server"/>

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

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

Community Additions

ADD
Show:
© 2014 Microsoft