Blogs
 
Thursday, December 15, 2005

In an earlier post about ASP.NET 2.0 SiteMap and MenuControls, we discussed how to create menu controls from a sitemap. We ended by saying that we encountered a problem in which we could not find a way to make the parent tab stay highlighted when a sub-tab was selected.

We wanted this:

But instead settled with this:

After further investigation in my spare time, however, I did find a solution. So if you are all ready… here it is.

Protected Sub menuMain_PreRender(ByVal sender As Object, 
ByVal e As System.EventArgs) Handles menuMain.PreRender
Dim
node As SiteMapNode = SiteMap.CurrentNode If Not node.ParentNode.Equals(node.RootNode)
And node.ParentNode IsNot Nothing Then For Each item As MenuItem In menuMain.Items If item.DataPath = node.ParentNode.Key Then item.Selected = True End If Next End If End Sub

In the PreRender event of our parent menu control, we first need to check that our currently selected sitemap node has a parent node that is also not the root node.

If Not node.ParentNode.Equals(node.RootNode) 
And node.ParentNode IsNot Nothing Then

If this is the case then all we need to do is go through each item of our parent menu control and compare the DataPath value to our currently node's parent Key value. If these match, then we know that this menu item is the parent of the currently selected sub-item, and now we just have to set that item to be selected.

For Each item As MenuItem In menuMain.Items
    If item.DataPath = node.ParentNode.Key Then
	item.Selected = True
    End If
Next

And so now we can have what we always wanted... Tabs and their sub-tabs selected at the same time!

Cheers

Jason McEvoy



Archive

Recent Entries

Blogs we read


Page 1 Of 1 (1 items)