-[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_12_13 (VB)

MyTreePath.vb
 
Imports Microsoft.VisualBasic
Imports System.Web
Imports System.Web.UI.WebControls
Imports System.Web.UI

Namespace My

    Public Class TreePath
        
Inherits SiteMapPath

        
Private _tree As TreeView

        
Public Property TreeViewID() As String
            Get
                Dim o As String = CType(ViewState("TreeID"), String)
                
If o Is Nothing Then
                    Return String.Empty
                
Else
                    Return o
                
End If
            End Get
            Set(ByVal value As String)
                ViewState(
"TreeID") = value
            
End Set
        End Property

        Public Property Tree() As TreeView
            
Get
                If _tree Is Nothing Then
                    _tree = Page.FindControl(TreeViewID)
                
End If
                Return _tree
            
End Get
            Set(ByVal value As TreeView)
                _tree = value
            
End Set
        End Property

        Protected Overrides Sub CreateControlHierarchy()
            
Dim item As SiteMapNodeItem = Nothing

            ' Get the current selected tree node
            If Tree Is Nothing Then
                Throw New Exception("Tree or TreeViewID is not valid")
            
End If
            Dim node As TreeNode = Tree.SelectedNode()
            
If node Is Nothing Then
                Return
            Else
                ' Create the current node
                AddItemFromTreeNode(item, node, SiteMapNodeItemType.Current)
                node = node.Parent

                
' Create the parent nodes and separators
                While node IsNot Nothing AndAlso node.Parent IsNot Nothing
                    AddItemFromTreeNode(item, Nothing, SiteMapNodeItemType.PathSeparator)
                    AddItemFromTreeNode(item, node, SiteMapNodeItemType.Parent)
                    node = node.Parent
                
End While

                ' Create the root node (unless there is only one node)
                If node IsNot Nothing Then
                    AddItemFromTreeNode(item, Nothing, SiteMapNodeItemType.PathSeparator)
                    AddItemFromTreeNode(item, node, SiteMapNodeItemType.Root)
                
End If
            End If
        End Sub

        Private Sub AddItemFromTreeNode(ByRef item As SiteMapNodeItem, _
                                        
ByVal node As TreeNode, ByVal type As SiteMapNodeItemType)

            
' Instantiate a new SiteMapNodeItem
            item = New SiteMapNodeItem(0, type)
            
Dim smn As SiteMapNode = Nothing
            If node IsNot Nothing Then
                smn = New SiteMapNode(New XmlSiteMapProvider, "key", _
                                        node.NavigateUrl, node.Text, node.ToolTip)
            
End If

            ' Initializes the controls with styles/templates
            item.SiteMapNode = smn
            InitializeItem(item)

            
' Fire Creation and Databinding events
            OnItemCreated(New SiteMapNodeItemEventArgs(item))
            Controls.AddAt(0, item)
            item.DataBind()
            OnItemDataBound(
New SiteMapNodeItemEventArgs(item))

            
' Clean up
            item.SiteMapNode = Nothing
            item.EnableViewState = False
        End Sub

    End Class
End
Namespace
 
test.aspx
 
<%@ Page Language="VB" %>
<%
@ Register Namespace="My" TagPrefix="My" %>

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

<
script runat="server">

</
script>

<
html xmlns="http://www.w3.org/1999/xhtml">
<
head runat="server">
    <title>Untitled Page</title>
</
head>
<
body>
    <form id="form1" runat="server">
        <div>
            <asp:TreeView ID="TreeView1" runat="server" OnSelectedNodeChanged="TreeView1_SelectedNodeChanged">
                <Nodes>
                    <asp:TreeNode Text="a" Value="a" NavigateUrl="foo">
                        <asp:TreeNode Text="b" Value="b" NavigateUrl="foo"></asp:TreeNode>
                    </asp:TreeNode>
                    <asp:TreeNode Text="c" Value="c">
                        <asp:TreeNode Text="d" Value="d" NavigateUrl="foo">
                            <asp:TreeNode Text="e" Value="e"></asp:TreeNode>
                        </asp:TreeNode>
                    </asp:TreeNode>
                    <asp:TreeNode Text="f" Value="f" NavigateUrl="foo">
                        <asp:TreeNode Text="g" Value="g">
                            <asp:TreeNode Text="h" Value="h" NavigateUrl="foo">
                                <asp:TreeNode Text="j" Value="j"></asp:TreeNode>
                            </asp:TreeNode>
                        </asp:TreeNode>
                    </asp:TreeNode>
                </Nodes>
            </asp:TreeView>
            <My:TreePath runat="server" ID="TreePath1" TreeViewID="TreeView1" OnItemDataBound="foo">
                <CurrentNodeStyle BackColor="Green" />
                <RootNodeTemplate>
                    RootNode:
                    <%
#Eval("Title")%>
                    Url:
                    <%
# Eval("Url") %>
                
</RootNodeTemplate>
            </My:TreePath>
        </div>
    </form>
</
body>
</
html>
Posted: Dec 13 2005, 11:39 AM by dannychen | with 4 comment(s)
Filed under:

Comments

michaelg said:

Can you provide the source code for "TreeView1_SelectedNodeChanged" and "foo" methods as well ?

# January 29, 2008 2:06 PM

michaelg said:

Can I use the Menu control instead of TreView ? If yes, how ?

Thanks.

# January 29, 2008 4:01 PM

SuperRobot5000 said:

Great post!!  Worked well for me.  :0)

# June 19, 2008 6:13 PM

Damalis said:

Hi guys. Treat all disasters as if they were trivialities but never treat a triviality as if it were a disaster. Help me! I can not find sites on the: Inderal for migraine prevention side effects. I found only this - <a href="www.w2p.org/.../Inderal">enalapril vs inderal</a>. Also, begin about to severe bowel and according out also not, inderal. Inderal, what you messed was doctor meds widening to pay their goals sweaty in the companies, but they could much, you took sleeping and working. Thanks ;-). Damalis from Monaco.

# April 2, 2010 9:59 AM
Leave a Comment

(required) 

(required) 

(optional)

(required)