Use the Node Iterator

  • 2 min to read

The ASPxTreeList introduces the Node Iterator, which allows you to traverse through the nodes and avoid writing recursive code.

To use the Node Iterator you should create a new instance of the TreeListNodeIterator object. A start node is specified within the constructor.


TreeListNodeIterator iterator = new TreeListNodeIterator(startNode);

To start traversing from the root node, pass the ASPxTreeList.RootNode as a parameter.


TreeListNodeIterator iterator = new TreeListNodeIterator(treeList.RootNode);

You can also use the ASPxTreeList.CreateNodeIterator method to create the Nodes Iterator.


TreeListNodeIterator iterator = treeList.CreateNodeIterator(startNode);

// Starts traversing from the root node
TreeListNodeIterator iteratorFromRoot = treeList.CreateNodeIterator();

The current node is returned by the TreeListNodeIterator.Current property. To obtain the next node, use the TreeListNodeIterator.GetNext method.

To return the Node Iterator to its default state, call the TreeListNodeIterator.Reset method.

Example

This example shows how to collect the key values of all parent nodes displayed within the ASPxTreeList. The TreeListNodeIterator object is used to traverse through the nodes.

using System.Collections.Generic;
using DevExpress.Web.ASPxTreeList;

List<string> nodeKeys;
void ProcessNodes(TreeListNode startNode) {
    if (startNode == null) return;
    TreeListNodeIterator iterator = new TreeListNodeIterator(startNode);
    nodeKeys = new List<string>();
    while (iterator.Current != null) {
        GetParentNodeKey(iterator.Current);
        iterator.GetNext();
    }
}

private void GetParentNodeKey(TreeListNode node) {
    if (node != treeList.RootNode && node.HasChildren)
        nodeKeys.Add(node.Key);
}