Click here to Skip to main content
12,815,986 members (38,593 online)
Click here to Skip to main content
Add your own
alternative version


71 bookmarked
Posted 13 Aug 2006

Using Menu and Sitemap with Master Page

, 13 Aug 2006 CPOL
Rate this:
Please Sign up or sign in to vote.
An article on how to use Menu and Sitemap data source controls with Master pages to create a Master - Child navigation menu.


This article explains how to use Sitemap with Master pages to create a Master-Child navigation menu.

Scenario: We have pages with a top menu bar and a side menu in which the side menu displays pages/links based on what is selected in the top menu.

In this article, I will explain how to achieve this in ASP.NET 2.0. For an example, assume the following requirements:

  1. We have main menu links such as Home, Products, and Services.
  2. When the Products link is clicked, the Products.aspx page should be displayed. In Products.aspx, the left menu should display links to different product pages like Product1.aspx, Products2.aspx, and Products3.aspx.
  3. When the Services link is clicked, the Services page should be displayed. In the Services.aspx page, the left menu should display different service pages like Service1.aspx, Service2.aspx, and Service3.aspx.
  4. Maintain the left menu listing when any of the pages in the products/services left menu is clicked.

This can be achieved by using a sitemap file, sitemap data source, and the menu controls available in ASP.NET 2.0.

The code

First of all, we need to create a sitemap file as shown below:

<?xml version="1.0" encoding="utf-8" ?>
<siteMap xmlns="" >
    <siteMapNode url="default.aspx" title="Home"  description="Sitemap example's home page">
      <siteMapNode url="products.aspx" title="Products"  description="Products listing page">
        <siteMapNode url="products/product1.aspx" title="Product 1"  description="" />
        <siteMapNode url="products/product2.aspx" title="Product 2"  description="" />
        <siteMapNode url="products/product3.aspx" title="Product 3"  description="" />
      <siteMapNode url="services.aspx" title="Services"  description="Services listing page" >
        <siteMapNode url="services/service1.aspx" title="Services 1"  description="" />
        <siteMapNode url="services/service2.aspx" title="Services 2"  description="" />
        <siteMapNode url="services/service3.aspx" title="Services 3"  description="" />

Now create a master page and format it as per your requirements. Then add two sitemap data source controls and name the controls smdsMaster and smdsChild.

For the smdsChild control, set the following properties:

  • ShowStartingNode -> False
  • StartFromCurrentNode -> True

Then add a menu control in the top section of the master page, name it mnuMaster, and apply any auto-formats available. Set the following properties of the smdsMaster control:

  • DataSource –> smdsMaster
  • MaximumDynamicDisplay -> 0
  • Orientation -> Horizontal
  • StaticDisplayLevels -> 2

Add a menu control in the left section of the master page, name it mnuChild, and set the following property of the control:

  • DataSource -> smdsChild

Now your master page may look some thing like below:

Master Page design mode

Add three child pages (which derive from the above MasterPage) named Default.aspx, Products.aspx, and Services.aspx in the project root. Also add two folders named Products and Services under the root folder. Create Product1.aspx, Product2.aspx, and Product3.aspx under the Products folder, and Service1.aspx, Service2.aspx, and Service3.aspx under the Services folder, as shown below:

Master Page design mode

In the home page, you may want to hide the child menu and also display the left menu even when any of the child pages in the products or services category is selected. For this, you can add the code below in the master page’s load event, as I did in the sample application.

protected void Page_Load(object sender, EventArgs e)
    if (!IsPostBack)
        string sURL = Request.Url.ToString().ToLower();
        if (sURL.EndsWith("default.aspx"))
            mnuChild.Visible = false;
        else if (sURL.Contains("/products/") || 
            smdsChild.StartingNodeOffset = -1;

Now run the application and check the navigation menus in each page. You may get screens similar to those shown below.

Products Page

Products child pages

Services Page

Hope you find this article informative.


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

Nate K
United States United States
Nate has been in the IT industry for more than a decade.

He loves music and listens to all kinds of good music.

You may also be interested in...


Comments and Discussions

GeneralMy vote of 2 Pin
pinky_sood1-May-11 11:24
memberpinky_sood1-May-11 11:24 
GeneralNice...but Pin
simpa7-Mar-11 11:25
membersimpa7-Mar-11 11:25 
GeneralIs there a way to hook up "On clicked" events to this menu Pin
M i s t e r L i s t e r13-Nov-09 9:45
memberM i s t e r L i s t e r13-Nov-09 9:45 
Generalemail Pin
santosh tirunagaro29-Sep-09 0:55
membersantosh tirunagaro29-Sep-09 0:55 
GeneralParent node HIGHLIGHT after selecting child node (sitemapwithmasterpage) Pin
santosh tirunagaro29-Sep-09 0:53
membersantosh tirunagaro29-Sep-09 0:53 
GeneralVB update to keep child menu visable Pin
Larry Ruehl25-Sep-09 4:28
memberLarry Ruehl25-Sep-09 4:28 
Very nice job on this one! Took me a while to understand, but when I got it working, WOW!

However I had trouble with you C code getting it to work. For those of you coding with VB here is my trick.

Put this code in your master page:

Partial Class MasterPage
Inherits System.Web.UI.MasterPage

Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Load
If Not IsPostBack Then
Dim sURL As String = Request.Url.ToString().ToLower()

'Input starting pages with no child menu's. Note: tolower above converts to lower case so dont use any upper case in "xxx.aspx"
If sURL.EndsWith("mainpage.aspx") OrElse sURL.EndsWith("logout.aspx") OrElse sURL.EndsWith("projectlist.aspx") Then
MenuChild.Visible = False
'Input Starting pages with Child Menus. Since you cant duplicate urls in web.sitemap I set up a main page with a redirect to the first page.
If sURL.EndsWith("vendorform.aspx") OrElse sURL.EndsWith("loginmain.aspx") Then
MenuChild.Visible = True

'This handles all child pages. when you click on a child page that is not the starting page from the master, this will keep the Child desplay menu
smdsChild.StartingNodeOffset = -1
MenuChild.Visible = True
End If

End If
End If

End Sub

End Class

Tada, it works for me.

Hope that helps someone. To the author of this post, Thank you very much!
GeneralThank you Pin
Roshan P Mohammed11-Apr-09 5:01
memberRoshan P Mohammed11-Apr-09 5:01 
GeneralNice one Pin
menonsk21-Oct-07 11:46
membermenonsk21-Oct-07 11:46 
GeneralNice but not DataSource... Pin
gubijo8-Sep-07 14:05
membergubijo8-Sep-07 14:05 
GeneralChild menu keeps disappearing Pin
fiddlewidget25-Aug-07 16:14
memberfiddlewidget25-Aug-07 16:14 
GeneralWhen I select child node appropriate main node is not selected Pin
VentureSerShk12-Feb-07 3:13
memberVentureSerShk12-Feb-07 3:13 
GeneralRe: When I select child node appropriate main node is not selected Pin
Henrik Sterndorff Jessen1-Jun-10 11:39
memberHenrik Sterndorff Jessen1-Jun-10 11:39 
QuestionNice. But i have a question. Pin
Baloreska14-Aug-06 21:03
memberBaloreska14-Aug-06 21:03 
AnswerRe: Nice. But i have a question. Pin
Ghostnet15-Aug-06 7:23
memberGhostnet15-Aug-06 7:23 
GeneralRe: Nice. But i have a question. Pin
Andre K15-Aug-06 20:25
memberAndre K15-Aug-06 20:25 
GeneralRe: Nice. But i have a question. Pin
Ghostnet15-Aug-06 20:46
memberGhostnet15-Aug-06 20:46 
GeneralRe: Nice. But i have a question. Pin
Andre K15-Aug-06 22:26
memberAndre K15-Aug-06 22:26 
GeneralRe: Nice. But i have a question. Pin
Ghostnet15-Aug-06 22:56
memberGhostnet15-Aug-06 22:56 
GeneralRe: Nice. But i have a question. Pin
SreeluMandiga22-Oct-08 22:40
memberSreeluMandiga22-Oct-08 22:40 
GeneralRe: Nice. But i have a question. Pin
SreeluMandiga22-Oct-08 22:41
memberSreeluMandiga22-Oct-08 22:41 
GeneralRe: Nice. But i have a question. [modified] Pin
Nataraj K16-Aug-06 8:12
memberNataraj K16-Aug-06 8:12 
GeneralRe: Nice. But i have a question. Pin
Baloreska23-Aug-06 22:05
memberBaloreska23-Aug-06 22:05 
GeneralRe: Nice. But i have a question. Pin
mhbearlover5-Aug-10 0:37
membermhbearlover5-Aug-10 0:37 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web01 | 2.8.170308.1 | Last Updated 13 Aug 2006
Article Copyright 2006 by Nate K
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid