Solution - Treeview Site Navigation without ShowExpandCollapse


T

theintrepidfox

Dear Group

Just thought of posting a solution on how to use an Microsoft ASP.NET
2.0 treeview control for simple site navigation without the need of
using the Expand/Collapse image click option.
Instead Treeview expands (or not) when a treenode text is clicked. Note
that it's probably not foolproof (not tested) but it works for me.

If you have any ideas of improvement, please post the code here or
email me.

Hth and gets you started,

Martin

What you need:
- A Sitemap. Each node must point to a local aspx.
- SiteMapDataSource and a SiteMapProvider
- Treeview on your master page (called mnuSideNav in this example)
- Place the following in your master code beside:

Protected Sub TreeView1_DataBound(ByVal sender As Object, ByVal e
As System.EventArgs) Handles mnuSideNav.DataBound

Dim SitemapNode As SiteMapNode =
SiteMapDataSource1.Provider.CurrentNode

'Build Current SiteMapPath To Root
'- Add Current Node
Dim Path(0) As String
Path.SetValue(SitemapNode.Title.ToString, 0)
'- Work Way Up To Root, Adding ParentNodes
Dim i As Integer = 1
While SitemapNode.ParentNode.Title <> ""
ReDim Preserve Path(i)
Path.SetValue(SitemapNode.ParentNode.Title.ToString, i)
SitemapNode = SitemapNode.ParentNode
i += 1
End While
'- Reverse Array To Reflect Root To CurrentNode
Array.Reverse(Path)

'Find TreeNodes in Array And Expand Tree
Dim TreeNode As TreeNode
Dim ii As Integer = 0
Dim TreePath As String
While ii < Path.Length
TreePath += Path.GetValue(ii) 'Set To First Value in Path
Array Or Ammdend Treepath.
TreeNode = mnuSideNav.FindNode(TreePath)
'Expand Current Node
TreeNode.Expand()
'Ammend Tree ValuePath
TreePath += mnuSideNav.PathSeparator.ToString
ii += 1
End While

End Sub
 
Ad

Advertisements

S

sandhyanp

Hi.

So does this open the url on the node as well as expand/collapse the
node when you click on it?
Do you have this working somewhere so I can see it in action?

Thanks,
Sandhya.
 
Ad

Advertisements

T

theintrepidfox

Hi Sandhya

Yes, it does. You may view a sample on http://www.freedom2drive.net
although this sitemap does not contaion any childnodes it works with
childnodes to any level.

The only thing it doesn't do is if you have roles and security trimming
enabled. I'm working actually right now on a VB.NET update that will
store the sitemap in SQL server and allows you to use it in conjunction
with roles and security trimming.

Contact me on (e-mail address removed) in case you have any questions.

All the best,

Martin
Invent2b Group
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top