Highlight root node of Menu when browsing child node

Last post 04-05-2006 9:25 PM by fatewong. 5 replies.

Sort Posts:

  • Highlight root node of Menu when browsing child node

    03-11-2006, 12:07 AM
    • Member
      718 point Member
    • fatewong
    • Member since 12-08-2004, 3:45 PM
    • Posts 121
    I had been using web.sitemap with Menu control for navigation purpose. The following is my web.sitemap.

    <siteMapNode url="~/index.aspx" title="Home"  description="Home">
         <siteMapNode url="" title="City" description="City">
             <siteMapNode url="~/city_list.aspx" title="City List"  description="City List" />
             <siteMapNode url="~/city_entry.aspx" title="City Entry"  description="City Entry" />
         </siteMapNode>
         <siteMapNode url="" title="State" description="State">
             <siteMapNode url="~/state_list.aspx" title="State List"  description="State List" />
             <siteMapNode url="~/state_entry.aspx" title="State Entry"  description="State Entry" />
         </siteMapNode>
    </siteMapNode>


    I bind it to SiteMapDatasource and thus bind to Menu control. Is there any way to highlight the root node (eg. City, State) instead of highlighting its child node (eg. City List, City Entry, State List, State entry)
     when we are browsing the child node on web.
  • Re: Highlight root node of Menu when browsing child node

    03-14-2006, 2:57 PM
    • Contributor
      4,347 point Contributor
    • dannychen
    • Member since 08-24-2004, 12:17 PM
    • Redmond, WA
    • Posts 840
    • AspNetTeam
      Moderator

    I have heard that in the MenuItemDataBound event, if you determine that the current Node is the selected one, you can set the parent to selected as well and it will apply the selected node style.  Ironically this would be in someways a bug since multiple selected nodes would exist but since it's not filed, it likely won't get fixed.  In general a "parent selected style" is something certainly on the plate for enhancements to the Menu control in the future.

    --
    Danny

    disclaimer: Information provided is 'as is' and conveys no warranties or guarantees.
  • Re: Highlight root node of Menu when browsing child node

    03-14-2006, 3:00 PM
    Answer
    • Member
      155 point Member
    • ryanr
    • Member since 01-06-2006, 9:06 PM
    • Posts 25
    Surely. Here's what I do to always select the 2nd level node regardless of how deep the user is in the SiteMap (or Home, since I flatten the first two levels for my menu.)

        Protected Sub mnuHead_DataBound(ByVal sender As Object, ByVal e As System.EventArgs) Handles mnuHead.DataBound
            If Not SiteMap.CurrentNode Is Nothing Then
                Dim node As SiteMapNode
                Dim prev As SiteMapNode 'ultimately the "level 2" root of whatever the current node is
                node = SiteMap.CurrentNode
                prev = SiteMap.CurrentNode
                While Not node.ParentNode Is Nothing
                    prev = node
                    node = node.ParentNode
                End While
                Dim ParentMenu As MenuItem
                Dim ChildMenu As MenuItem

                For Each ParentMenu In mnuHead.Items
                    ' only one ParentMenu for me.. home
                    If ParentMenu.NavigateUrl = prev.Url Then
                        Exit For
                    End If
                    ' this is the level i want selected
                    For Each ChildMenu In ParentMenu.ChildItems
                        If ChildMenu.NavigateUrl = prev.Url Then
                            ChildMenu.Selected = True
                            Exit For
                        End If
                    Next
                Next
            End If
        End Sub

  • Re: Highlight root node of Menu when browsing child node

    03-14-2006, 7:42 PM
    • Contributor
      2,898 point Contributor
    • jminond
    • Member since 07-21-2003, 6:33 PM
    • New York
    • Posts 608
    in he item menu data bind....
    you could do something like
    if (e.item node == sitemap.currentnode.parent)
              item.text = "<span class=\"selectedItemCSS\">" + text + "</span>";

    check the syntax online at http://www.msdn.com or scott guthries blog for sure, but more orless that is what you will want to do.
    Jonathan Minond
    http://www.Jonavi.com
    http://www.jonavi.com/Default.aspx?pageID=21
    http://RainbowBeta.com
    http://community.rainbowportal.net/blogs/jonathans_rainbow_blog/default.aspx
    http://dotnetslackers.com/community/blogs/jminond/default.aspx
  • Re: Highlight root node of Menu when browsing child node

    03-30-2006, 3:45 AM
    • Member
      680 point Member
    • vishal_0009
    • Member since 09-15-2005, 1:20 PM
    • Posts 136
    Hello there,

    I am trying to implement the same thing. But couldn't find answer to this and come across to your post.

    If you've implemented this or sorted this problem, can you please tell me that how did u do it. I really want to solve this. If there's any sample code or link, then can you plz provide me that.

    I would welcome to hearing from you.

    Many thnx,

    vishal.

  • Re: Highlight root node of Menu when browsing child node

    04-05-2006, 9:25 PM
    • Member
      718 point Member
    • fatewong
    • Member since 12-08-2004, 3:45 PM
    • Posts 121
    Vishal had posted one question for me which is about how to select the 3rd level node instead of 2nd level node. Since this question is same for my post, I had modified the solution provided by ryanr to adapt this situation. Vishal's menu view is as follow and he wants to highlight 3rd level node (Course List) instead of 2nd level node when he browses to 4th level node (Course Details).

    Home

       Courses

          Course List

             Course Details

    The solution is shown below:

        Protected Sub Menu1_DataBound(ByVal sender As Object, ByVal e As System.EventArgs) Handles Menu1.DataBound
            If Not SiteMap.CurrentNode Is Nothing Then
                Dim node As SiteMapNode
                Dim prev As SiteMapNode
                node = SiteMap.CurrentNode
                prev = SiteMap.CurrentNode

                While Not node.ParentNode.ParentNode Is Nothing
                    prev = node
                    node = node.ParentNode
                End While

                Dim ParentMenu As MenuItem
                Dim ChildMenu As MenuItem
                Dim SubChildMenu As MenuItem

                For Each ParentMenu In Menu1.Items
                    ' only one ParentMenu for me.. home
                    If ParentMenu.NavigateUrl = prev.Url Then
                        Exit For
                    End If
                    ' the second level is my choice too
                    For Each ChildMenu In ParentMenu.ChildItems
                        If ChildMenu.NavigateUrl = prev.Url Then
                            Exit For
                        End If

                        '
    this is the 3rd level to be selected
                        For Each SubChildMenu In ChildMenu.ChildItems
                            If SubChildMenu.NavigateUrl = prev.Url Then
                                SubChildMenu.Selected = True
                                Exit For
                            End If
                        Next
                    Next
                Next
            End If
        End Sub
Page 1 of 1 (6 items)