Thursday, 13 March 2014

ASP.net menu only show certain nodes in Web.sitemap

In this article I will explain how to show certain nodes in web.sitemap page in ASP.NET

In ASP.NET the menu can be stored in a file to make it easier to maintain. This file is normally called web.sitemap, and is stored in the root directory of the web. It’s very useful for Page navigation. If you want web.sitemap page design please click here.

ASP.net menu only show certain nodes in Web.sitemap menu control we need to write code in Master page. Need to add Menu control and databound event. Please see the below code for Menu control design.

<asp:Menu ID="Menu1" runat="server" DataSourceID="SiteMapDataSource1"
onmenuitemdatabound="Menu1_MenuItemDataBound">
</asp:Menu>
<asp:SiteMapDataSource ID="SiteMapDataSource1" runat="server" ShowStartingNode="false" />

Below is the Menu databound event code.

protected void Menu1_MenuItemDataBound(object sender, System.Web.UI.WebControls.MenuEventArgs e)
    {      
        System.Web.UI.WebControls.Menu menu = (System.Web.UI.WebControls.Menu)sender;
        SiteMapNode mapNode = (SiteMapNode)e.Item.DataItem;
      
            if (mapNode.Url == string.Empty || mapNode.Url.Contains("Home.aspx")
                || mapNode.Url.Contains("ReportList.aspx")
                )                
            {               
            }
            else
            {
                //Below code is useful for removing unnecessary pages. 
                System.Web.UI.WebControls.MenuItem itemToRemove = e.Item;
                itemToRemove.Parent.ChildItems.Remove(itemToRemove);
            }
        }  

    }


No comments:

Post a Comment