Extract All TreeView Nodes

This article is written by Pon Saravanan  on 07-May-10 Last modified on :07-May-10

Recursively scan and access all TreeView nodes

TreeView nodes can be accessed from either Nodes collection from the TreeView root or ChildNodes collection for a TreeNode. But you don’t have any native functions to list all nodes to a single level list. This article explains how to get all nodes with complete source code in VB.Net.

TreeView Preparations

To distinguish the levels I have made the lines visible by the property ShowLines as visible. I have used two DIVs to show the output and the TreeView side by side. Sitemap is going to be used for the sample data to bind the TreeView, So a SiteMapDataSource to bind the Sitemap to the TreeView.

Sample Data

To explain accessing all nodes in all levels of hierarchy, we need some sample data to fill up all levels.  Here in our case we are going to deal that with a sitemap. So I have purposely created a sitemap with few levels and each level contains few items. I have named them in a way we can identify its level from looking at the name. Look the screen capture for further understandings.

Screen Capture


Source Code

Markup (*.aspx)

<%@ Page Language="VB" AutoEventWireup="false"
    CodeFile="Default.aspx.vb" Inherits="_Default" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <form id="form1" runat="server">
    <asp:Button runat="server" ID="Button1" Text="List All" />
    <br />
    <div style="float: left; width: 20%">
        <asp:TreeView runat="server" ID="TreeView1" ShowLines="true">
    <div style="float:left;vertical-align:top">
            Result all nodes</h1>
        <asp:Label runat="server" ID="lblMsg"></asp:Label>

Code Behind(*.Aspx.Vb)

Partial Class _Default
    Inherits System.Web.UI.Page
    Protected Sub Button1_Click(ByVal sender As Object, _
                                ByVal e As System.EventArgs) Handles Button1.Click
        For Each ChildNode As TreeNode In TreeView1.Nodes
    End Sub
    Protected Sub Page_Load(ByVal sender As Object, _
                            ByVal e As System.EventArgs) Handles Me.Load
    End Sub
    Private Sub LoadTreeview()
        Dim SampleData As New SiteMapDataSource()
        TreeView1.DataSource = SampleData
    End Sub
    Private Sub ListAll(ByVal ParentNode As TreeNode)
        lblMsg.Text &= ParentNode.Text & "<br />"
        For Each ChildNode As TreeNode In ParentNode.ChildNodes
            lblMsg.Text &= ChildNode.Text & "<br />"
            If (ChildNode.ChildNodes.Count > 0) Then ListAll(ChildNode)
    End Sub
End Class

Sample Data

<?xml version="1.0" encoding="utf-8" ?>
<siteMap xmlns="http://schemas.microsoft.com/AspNet/SiteMap-File-1.0" >
  <siteMapNode title="1" >
    <siteMapNode title="1-1" >
      <siteMapNode title="1-1-1" />
      <siteMapNode title="1-1-2" />
    <siteMapNode title="1-2" >
      <siteMapNode title="1-2-1" />
      <siteMapNode title="1-2-2" />
    <siteMapNode title="1-3" >
      <siteMapNode title="1-3-1" />
    <siteMapNode title="1-4" >
      <siteMapNode title="1-4-1" />
    <siteMapNode title="1-5" >
      <siteMapNode title="1-5-1" />


Captcha Image
For you specially:  
Captcha Text Enter the text in the image.(Not Case sensitive)    

Spam Bot Trap


Select Theme