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"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <asp:Button runat="server" ID="Button1" Text="List All" />
    <br />
    <div style="float: left; width: 20%">
        <h1>
            Treeview</h1>
       
        <asp:TreeView runat="server" ID="TreeView1" ShowLines="true">
        </asp:TreeView>
    </div>
    <div style="float:left;vertical-align:top">
        <h1>
            Result all nodes</h1>
        <asp:Label runat="server" ID="lblMsg"></asp:Label>
    </div>
    </form>
</body>
</html>

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
            ListAll(ChildNode)
        Next
    End Sub
    Protected Sub Page_Load(ByVal sender As Object, _
                            ByVal e As System.EventArgs) Handles Me.Load
        LoadTreeview()
    End Sub
    Private Sub LoadTreeview()
        Dim SampleData As New SiteMapDataSource()
        TreeView1.DataSource = SampleData
        TreeView1.DataBind()
    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)
        Next
    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>
    <siteMapNode title="1-2" >
      <siteMapNode title="1-2-1" />
      <siteMapNode title="1-2-2" />
    </siteMapNode>
    <siteMapNode title="1-3" >
      <siteMapNode title="1-3-1" />
    </siteMapNode>
    <siteMapNode title="1-4" >
      <siteMapNode title="1-4-1" />
    </siteMapNode>
    <siteMapNode title="1-5" >
      <siteMapNode title="1-5-1" />
    </siteMapNode>
  </siteMapNode>
</siteMap>











Comments

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



Spam Bot Trap



   



Select Theme
White
Blue
Brown
Gray