-[Danny Chen]- Blog of an ASP.NET QA tester

Tips and info about Site Navigation, ImageMap, Menu and other cool ASP.NET v2.0 features.

Code Listing for 2005_10_20.aspx (VB)

<%@ Page Language="VB" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<script runat="server">

    ' Recursively searches the tree for all nodes at a given depth
    ' and adds them to a list of TreeNodes
    Private Sub FindNodesAtDepth(ByVal collection As TreeNodeCollection, _
                                 ByVal targetDepth As Integer, _
                                 ByVal targetNodes As Generic.List(Of TreeNode))
       
        For Each node As TreeNode In collection
            If node.Depth = targetDepth Then
                targetNodes.Add(node)
            ElseIf node.Depth < targetDepth Then
                FindNodesAtDepth(node.ChildNodes, targetDepth, targetNodes)
            End If
        Next
    End Sub
   
    ' Recursively creates a clone of a given TreeNode Hierarchy   
    Private Sub CloneTreeNodeHierarchyRecursive(ByVal inCollection As TreeNodeCollection, _
                                                ByVal outCollection As TreeNodeCollection)
       
        For Each node As TreeNode In inCollection
            Dim clonedNode As TreeNode = CType(node, ICloneable).Clone()           
            outCollection.Add(clonedNode)
           
            If node.ChildNodes.Count > 0 Then
                CloneTreeNodeHierarchyRecursive(node.ChildNodes, clonedNode.ChildNodes)
            End If                       
        Next
    End Sub
   
    ' Databound event handler for a TreeView   
    Protected Sub TreeView_DataBound(ByVal sender As Object, ByVal e As System.EventArgs)
        Dim Tree As TreeView = sender
       
        ' Only prune the tree if a node is selected
        If Tree.SelectedNode IsNot Nothing Then
           
            Dim childNodes As New Generic.List(Of TreeNode)()
            Dim childNodeCollection As New TreeNodeCollection()
           
            FindNodesAtDepth(Tree.Nodes, Tree.SelectedNode.Depth, childNodes)
                       
            ' Copy from List(of TreeNode) to TreeNodeCollection
            For Each node As TreeNode In childNodes
                childNodeCollection.Add(node)
            Next

            ' Clear the TreeView nodes and add the nodes we found
            Tree.Nodes.Clear()
            CloneTreeNodeHierarchyRecursive(childNodeCollection, Tree.Nodes)
           
        End If
       
       
    End Sub
   
</script>

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
    <title>Demo Page</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <asp:TreeView ID="TreeView1" runat="server" DataSourceID="SiteMapDataSource1" OnDataBound="TreeView_DataBound">
        </asp:TreeView>
        <asp:SiteMapDataSource ID="SiteMapDataSource1" runat="server" />
       
    </div>
    </form>
</body>
</html>

Posted: Oct 20 2005, 10:16 AM by dannychen | with 17 comment(s)
Filed under:

Comments

john@hotmail.com said:

# August 31, 2009 11:36 AM

john@hotmail.com said:

# August 31, 2009 2:57 PM

john@hotmail.com said:

# September 1, 2009 3:13 AM

john@hotmail.com said:

# September 1, 2009 10:59 AM

john@hotmail.com said:

# September 1, 2009 1:42 PM

john@hotmail.com said:

# September 2, 2009 4:34 AM

john@hotmail.com said:

# September 2, 2009 4:49 AM

john@hotmail.com said:

qqdwczui.hostse.com/index.html

# September 2, 2009 8:34 PM

john@hotmail.com said:

qqdwczui.hostse.com/index.html

# September 3, 2009 8:26 AM

john@hotmail.com said:

# September 3, 2009 5:38 PM

john@hotmail.com said:

# September 4, 2009 7:58 AM

john@hotmail.com said:

# September 9, 2009 9:32 PM

john@hotmail.com said:

http://

http://

http://

http://

http://

http://

forums.nvidia.com/index.php

# September 11, 2009 1:37 PM

john@hotmail.com said:

# September 20, 2009 4:11 AM

prareptissere said:

All pizza places of USA pizza-us.com/.../98682

Find your best pizza.

# September 2, 2010 8:32 PM

ChetteEresy said:

Overview of US restaurants.  <a href="restaurants-us.com/.../a>

# January 25, 2011 12:56 AM

Alcazzarre said:

Maybe Alcazzarre could help you to solve your problems? You'r welcome pal.

# March 17, 2011 11:31 AM
Leave a Comment

(required) 

(required) 

(optional)

(required)