Back to Devexpress

TreeListNodeIterator Class

aspnet-devexpress-dot-web-dot-aspxtreelist-5ff46287.md

latest6.5 KB
Original Source

TreeListNodeIterator Class

Represents an object that enables you to traverse through nodes displayed within the ASPxTreeList.

Namespace : DevExpress.Web.ASPxTreeList

Assembly : DevExpress.Web.ASPxTreeList.v25.2.dll

NuGet Package : DevExpress.Web

Declaration

csharp
public class TreeListNodeIterator
vb
Public Class TreeListNodeIterator

The following members return TreeListNodeIterator objects:

Remarks

Basically, you will need to write recursive code to visit all the nodes contained within the ASPxTreeList. The Nodes Iterator allows you to avoid this, and makes traversing through the nodes a trivial task. All you have to do is to create a new TreeListNodeIterator object, and use its TreeListNodeIterator.GetNext method to obtain the next node.

A start node is specified within the TreeListNodeIterator ‘s constructor. To start traversing from the root node, pass the ASPxTreeList.RootNode as a parameter. The current node is returned by the TreeListNodeIterator.Current property.

To clear any explicitly defined properties and reset the TreeListNodeIterator object to its original, use the TreeListNodeIterator.Reset method.

Note

A Node Iterator can also be created using the ASPxTreeList’s ASPxTreeList.CreateNodeIterator 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.

aspx
<dx:ASPxTreeList ID="treeList" runat="server" Width="100%" KeyFieldName="ID" ParentFieldName="Parent_ID"
    AutoGenerateColumns="False" OnDataBound="treeList_DataBound">
    <Columns>
        <dx:TreeListDataColumn FieldName="Title" VisibleIndex="0" />
    </Columns>
</dx:ASPxTreeList>
csharp
using DevExpress.Web.ASPxTreeList;
using System;
using System.Collections.Generic;
using System.Data;

protected void Page_Load(object sender, EventArgs e) {
    DataTable dt = new DataTable();
    dt.Columns.Add("ID");
    dt.Columns.Add("Parent_ID");
    dt.Columns.Add("Title");

    dt.Rows.Add(dt.NewRow());
    dt.Rows[dt.Rows.Count - 1]["ID"] = 1;
    dt.Rows[dt.Rows.Count - 1]["Parent_ID"] = null;
    dt.Rows[dt.Rows.Count - 1]["Title"] = "One";

    dt.Rows.Add(dt.NewRow());
    dt.Rows[dt.Rows.Count - 1]["ID"] = 4;
    dt.Rows[dt.Rows.Count - 1]["Parent_ID"] = 1;
    dt.Rows[dt.Rows.Count - 1]["Title"] = "One A";

    dt.Rows.Add(dt.NewRow());
    dt.Rows[dt.Rows.Count - 1]["ID"] = 5;
    dt.Rows[dt.Rows.Count - 1]["Parent_ID"] = 1;
    dt.Rows[dt.Rows.Count - 1]["Title"] = "One B";

    treeList.DataSource = dt;
    treeList.DataBind();
}

protected void treeList_DataBound(object sender, EventArgs e) {
    ASPxTreeList list = sender as ASPxTreeList;
    ProcessNodes(list.Nodes[0]);
}
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);
}
vb
Imports DevExpress.Web.ASPxTreeList
Imports System
Imports System.Collections.Generic
Imports System.Data

Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs)
    Dim dt As System.Data.DataTable = New System.Data.DataTable()
    dt.Columns.Add("ID")
    dt.Columns.Add("Parent_ID")
    dt.Columns.Add("Title")
    dt.Rows.Add(dt.NewRow())
    dt.Rows(dt.Rows.Count - 1)("ID") = 1
    dt.Rows(dt.Rows.Count - 1)("Parent_ID") = Nothing
    dt.Rows(dt.Rows.Count - 1)("Title") = "One"
    dt.Rows.Add(dt.NewRow())
    dt.Rows(dt.Rows.Count - 1)("ID") = 4
    dt.Rows(dt.Rows.Count - 1)("Parent_ID") = 1
    dt.Rows(dt.Rows.Count - 1)("Title") = "One A"
    dt.Rows.Add(dt.NewRow())
    dt.Rows(dt.Rows.Count - 1)("ID") = 5
    dt.Rows(dt.Rows.Count - 1)("Parent_ID") = 1
    dt.Rows(dt.Rows.Count - 1)("Title") = "One B"
    treeList.DataSource = dt
    treeList.DataBind()
End Sub

Protected Sub treeList_DataBound(ByVal sender As Object, ByVal e As EventArgs)
    Dim list As ASPxTreeList = TryCast(sender, ASPxTreeList)
    ProcessNodes(list.Nodes(0))
End Sub

Private nodeKeys As List(Of String)

Private Sub ProcessNodes(ByVal startNode As TreeListNode)
    If startNode Is Nothing Then Return
    Dim iterator As TreeListNodeIterator = New TreeListNodeIterator(startNode)
    nodeKeys = New List(Of String)()

    While iterator.Current IsNot Nothing
        GetParentNodeKey(iterator.Current)
        iterator.GetNext()
    End While
End Sub

Private Sub GetParentNodeKey(ByVal node As TreeListNode)
    If Not node Is treeList.RootNode AndAlso node.HasChildren Then
        nodeKeys.Add(node.Key)
    End If
End Sub

Inheritance

Object TreeListNodeIterator TreeListFilterNodeIterator

See Also

TreeListNodeIterator Members

Use the Node Iterator

CreateNodeIterator(Boolean)

Tree List

DevExpress.Web.ASPxTreeList Namespace