Omit Items From Menu Using Web.sitemap

Last post 11-12-2008 7:56 AM by victorantos. 23 replies.

Sort Posts:

  • Omit Items From Menu Using Web.sitemap

    07-13-2007, 1:41 PM

    There are items in my Web.sitemap that I don't want to see on my Menu control. Is there an attribute I can add to the item in the sitemap to make it invisible or something?

     

    thanks

  • Re: Omit Items From Menu Using Web.sitemap

    07-14-2007, 4:13 AM
    • Member
      510 point Member
    • Biser
    • Member since 11-01-2006, 11:24 AM
    • Bulgaria
    • Posts 93

    HI

    You can use role management to hide this Items.

     

     

    Please remember to click "Mark as Answer" on this post if it helped you.
  • Re: Omit Items From Menu Using Web.sitemap

    07-16-2007, 10:56 AM

    Biser:

    HI

    You can use role management to hide this Items.

    Well I don't really want to block the content, the item in question just isnt relevant to that menu and I'm going to have a link to it somewhere else on the site. I just need a few specific items not to show up on the menu. It would be great if I could just add an attribute to the sitemapnodes to hide them.

  • Re: Omit Items From Menu Using Web.sitemap

    07-17-2007, 12:23 AM

    Hi,

    sebastiencouture:

    There are items in my Web.sitemap that I don't want to see on my Menu control. Is there an attribute I can add to the item in the sitemap to make it invisible or something?

    You can use the remove method of the menu to remove the items that you do not want to see from the menu's items. In fact, if you use the remove method, the items just only do not show in the menu control, but the sitemap file will not be changed.

     

    hope it helps.

    Amanda Wang
    Microsoft Online Community Support

    Please remember to mark the replies as answers if they help and unmark them if they provide no help.
  • Re: Omit Items From Menu Using Web.sitemap

    07-17-2007, 5:59 AM
    • All-Star
      16,809 point All-Star
    • Dave Sussman
    • Member since 06-17-2002, 11:53 AM
    • UK
    • Posts 2,333
    • ASPInsiders
      TrustedFriends-MVPs

    Amanda is right; this is exactly what I do for pages I don't want on the menu, but that I want to appear in my siteMapPath. I first add a custom attribute to my siteMapnode:

      <siteMapNode title="foo" url="foo.aspx" showInMenu="false" />

    You can add any custom attribute you like; in this case I've used 'showInMenu'. Next I handle the MenuItemDataBound event for the menu control, where I check for the attribute and if it is set to false, I remove the node from the menu:

    protected void Menu2_MenuItemDataBound(object sender, MenuEventArgs e)
    {
        SiteMapNode node = e.Item.DataItem as SiteMapNode;

        // check for the showInMenu attribute and if false
        // remove the node from the parent
        // this allows nodes to appear in the SiteMapPath but not show on the menu
        if (!string.IsNullOrEmpty(node["showInMenu"]))
        {
            bool isVisible;
            if (bool.TryParse(node["showInMenu"], out isVisible))
            {
                if (!isVisible)
                {
                    e.Item.Parent.ChildItems.Remove(e.Item);
                }
            }
        }
    }

    Let me know if you need this in VB.

     

    Dave

  • Re: Omit Items From Menu Using Web.sitemap

    07-17-2007, 7:35 AM

    Dave Sussman:

    Amanda is right; this is exactly what I do for pages I don't want on the menu, but that I want to appear in my siteMapPath. I first add a custom attribute to my siteMapnode:

      <siteMapNode title="foo" url="foo.aspx" showInMenu="false" />

    You can add any custom attribute you like; in this case I've used 'showInMenu'. Next I handle the MenuItemDataBound event for the menu control, where I check for the attribute and if it is set to false, I remove the node from the menu:

    protected void Menu2_MenuItemDataBound(object sender, MenuEventArgs e)
    {
        SiteMapNode node = e.Item.DataItem as SiteMapNode;

        // check for the showInMenu attribute and if false
        // remove the node from the parent
        // this allows nodes to appear in the SiteMapPath but not show on the menu
        if (!string.IsNullOrEmpty(node["showInMenu"]))
        {
            bool isVisible;
            if (bool.TryParse(node["showInMenu"], out isVisible))
            {
                if (!isVisible)
                {
                    e.Item.Parent.ChildItems.Remove(e.Item);
                }
            }
        }
    }

    Let me know if you need this in VB.

     

    Dave

    What language is that in? I need it in VB for asp.net

  • Re: Omit Items From Menu Using Web.sitemap

    07-17-2007, 10:38 PM

    Hi,

    The above code is C# code, you can transfer it to the VB Code by tools:

    Protected Sub Menu2_MenuItemDataBound(ByVal sender As Object, ByVal e As MenuEventArgs)
    Dim node As SiteMapNode = TryCast(e.Item.DataItem, SiteMapNode)

    ' check for the showInMenu attribute and if false
    ' remove the node from the parent
    ' this allows nodes to appear in the SiteMapPath but not show on the menu
    If Not String.IsNullOrEmpty(node("showInMenu")) Then
    Dim isVisible As Boolean
    If Boolean.TryParse(node("showInMenu"), isVisible) Then
    If Not isVisible Then
    e.Item.Parent.ChildItems.Remove(e.Item)
    End If
    End If
    End If
    End Sub

    hope it helps.

    Amanda Wang
    Microsoft Online Community Support

    Please remember to mark the replies as answers if they help and unmark them if they provide no help.
  • Re: Omit Items From Menu Using Web.sitemap

    07-18-2007, 8:24 AM

    thanks,

    So i put this code in App_code/hidemenu.vb like this and it doesnt seem to work. am I doing something  wrong?

     

    Imports Microsoft.VisualBasic
    
    Public Class showinmenu
        Protected Sub Menu2_MenuItemDataBound(ByVal sender As Object, ByVal e As MenuEventArgs)
            Dim node As SiteMapNode = TryCast(e.Item.DataItem, SiteMapNode)
            ' check for the showInMenu attribute and if false
            ' remove the node from the parent
            ' this allows nodes to appear in the SiteMapPath but not show on the menu
            If Not String.IsNullOrEmpty(node("showInMenu")) Then
                Dim isVisible As Boolean
                If Boolean.TryParse(node("showInMenu"), isVisible) Then
                    If Not isVisible Then
                        e.Item.Parent.ChildItems.Remove(e.Item)
                    End If
                End If
            End If
        End Sub
    End Class
     
  • Re: Omit Items From Menu Using Web.sitemap

    07-18-2007, 8:35 AM
    • All-Star
      16,809 point All-Star
    • Dave Sussman
    • Member since 06-17-2002, 11:53 AM
    • UK
    • Posts 2,333
    • ASPInsiders
      TrustedFriends-MVPs

    You need to place the code in the same page as the Menu control (same page if using inline code, or the code-behind page is using code-behind). This is usually the master page, but could be the code file for a user control if that's where your menu is.

    d

  • Re: Omit Items From Menu Using Web.sitemap

    07-18-2007, 8:45 AM
    • Member
      510 point Member
    • Biser
    • Member since 11-01-2006, 11:24 AM
    • Bulgaria
    • Posts 93

    HI

    You should put it into  Page code behind code where you declare your Menu2. Because this is event handler.

    Please remember to click "Mark as Answer" on this post if it helped you.
  • Re: Omit Items From Menu Using Web.sitemap

    07-18-2007, 10:06 AM

    Yeah thats what I did after and it still wont hide the item when I put showInMenu="false" in the sitemap. However, my menu control is not Menu2 its simply Menu so I changed that and still nothing. I know theres nothing wrong with my Master.page.vb because I have other code being executed in it just fine.

     

    thanks

  • Re: Omit Items From Menu Using Web.sitemap

    07-18-2007, 10:24 AM
    • All-Star
      16,809 point All-Star
    • Dave Sussman
    • Member since 06-17-2002, 11:53 AM
    • UK
    • Posts 2,333
    • ASPInsiders
      TrustedFriends-MVPs

    I suspect you need to hook this event into the Menu. Change the event declaration to add the Handles bit, like so:

    Protected Sub Menu2_MenuItemDataBound(ByVal sender As Object, ByVal e As MenuEventArgs) Handles Menu.MenuItemDataBound

    d

  • Re: Omit Items From Menu Using Web.sitemap

    07-18-2007, 2:13 PM

     

  • Re: Omit Items From Menu Using Web.sitemap

    07-18-2007, 2:13 PM

    Now I get a NullReferenceException on

     e.Item.Parent.ChildItems.Remove(e.Item)

     

  • Re: Omit Items From Menu Using Web.sitemap

    07-19-2007, 12:36 AM
    Answer
    • Member
      510 point Member
    • Biser
    • Member since 11-01-2006, 11:24 AM
    • Bulgaria
    • Posts 93

     HI sebastiencouture

    I tried and everything work just fine. I will try to summaries steps:

     
    First check your <asp:menu> control for OnMenuItemDataBound:

    <asp:Menu ID="Menu1" runat="server" OnMenuItemDataBound="Menu1_MenuItemDataBound" .... />

    Second add event handler Menu1_MenuItemDataBound into code behind code.

    protected void Menu1_MenuItemDataBound(object sender, MenuEventArgs e)
    {

     Third edit your web.sitemap and mark menu items which you don't want to be visible.

    <siteMapNode url="~/Test.aspx"      title="Test" showInMenu="false"/> 

    I hope this will help you.  

    Regards 

    Please remember to click "Mark as Answer" on this post if it helped you.
Page 1 of 2 (24 items) 1 2 Next >