How to: Use the Nodes Iterator
- 2 minutes to read
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.
<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>
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);
}