Click here to Skip to main content
Click here to Skip to main content

Using Multiple Sitemap Files with the jQuery Superfish Menu Plug-in

, 13 Feb 2009 CPOL
Rate this:
Please Sign up or sign in to vote.
Explains how you can dynamically generate a Superfish menu.



In Web Forms development, many developers almost always run into issues with the dynamically generated menus. One sample scenario is implementing the Membership API in your Web projects, where each role must have its own menu. There are many vendors that offer menus which can solve the problem, but why buy a suite just for a menu?


As you know, Microsoft already supports jQuery in VS 2008, and as announced in a blog post, VS would be shipping with the library. There are amazing jQuery plug-ins, and one of them is the Superfish menu plug-in by Joel Birch. I have used the idea of Doug Wilson from the article Using Multiple Sitemap Files in ASP.NET 2.0 and decided to use Sitemap files to describe menu structure. To load JavaScript scripts, I used the HttpCombiner handler of Omar Al Zabir: HTTP Handler to Combine Multiple Files...

Using the Code

I have created a custom control with the following markup:

<%@ Control Language="C#"
            Inherits="DynamicFishMenu.controls.XmlMenu" %>

    <ul class="sf-menu">
        <asp:Repeater ID="menuRepeater"
                    <%# getMenuNode(DataBinder.Eval(Container.DataItem, "url"),
                                    DataBinder.Eval(Container.DataItem, "title")) %>
                    <asp:Repeater ID="subMenuRepeater"
                            <%# getChildNode(DataBinder.Eval(Container.DataItem, "url"),
                                         DataBinder.Eval(Container.DataItem, "title"),
                                         DataBinder.Eval(Container.DataItem, "description"))%>
                            <asp:Repeater ID="subSubMenuRepeater"
                                    <%# getSubChildNode(
                                         DataBinder.Eval(Container.DataItem, "url"),
                                         DataBinder.Eval(Container.DataItem, "title"),
                                         DataBinder.Eval(Container.DataItem, "description"))%>

It is important to point out the <ul> tag and its class attribute value sf-menu.

Important value in control

I use nested Repeter controls with the XmlDataSource control to bind data and populate menu items. And the code-behind:

public void InitMenuDataSource(SiteMapMenus menu)
    XmlDataSource menuData = new XmlDataSource();
    menuData.EnableCaching = false;
    menuData.XPath = "siteMap/siteMapNode";
    menuData.DataFile = GetMapPath(menu);
    menuRepeater.DataSource = menuData;

string GetMapPath(SiteMapMenus menu)
    string serverMapPath = Server.MapPath("~");
    string path = String.Empty;
    switch (menu)
        case SiteMapMenus.MenuAdmin:
            path = serverMapPath + @"\maps\MenuAdmin.sitemap";
        case SiteMapMenus.MenuAcc:
            path = serverMapPath + @"\maps\MenuAcc.sitemap";
        case SiteMapMenus.MenuSimple:
            path = serverMapPath + @"\maps\MenuSimple.sitemap";
    return path;

There is a trick for generating sub menu items. I use the description-value attribute of siteMapNode for additional info to mark where to begin and finish a submenu.

protected string getSubChildNode(object url, object title, object desc)
    string resolvedUrl = Page.ResolveUrl(url.ToString());
    string numNode = desc.ToString();
    string link = String.Concat("<li><a href=\"", resolvedUrl, 
                    "\">", title.ToString(), "</a></li>");

    if (numNode.Equals("begin"))
        return String.Concat("<ul>", link);
    else if (numNode.Equals("end"))
        return String.Concat(link, "</ul></li>");
        return link;

Below is the markup definition of our control in MainPage.Master.

Imarkup definition of our control in MainPage

And the place you can trigger menus is:

protected void mainMenu_PreRender(object sender, EventArgs e)
    // comment above line and uncomment below to switch menus


As a final result, we have a flexible navigation for a website and a neat Superfish menu!

Happy coding!


  • 13 Feb 2009 - First post.


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


About the Author

Web Developer Sirma Solutions
Bulgaria Bulgaria
No Biography provided

Comments and Discussions

QuestionMouseover Tabs Menu PinmemberMS Babu10-May-13 20:49 
QuestionNeed Help please? Pinmembermetharam6-Feb-13 6:02 
GeneralMy vote of 4 PinmemberHoneyShyam8-Oct-11 1:58 
GeneralRestriction on Menu Pinmemberrockygorden1-Mar-10 3:43 
GeneralRe: Restriction on Menu Pinmemberpleykov1-Mar-10 4:32 
QuestionInstructions? PinmemberJBeckton4-Feb-10 15:20 
AnswerRe: Instructions? Pinmemberpleykov8-Feb-10 2:01 
Generalsub menu problem PinmemberDustin Hlava8-Jun-09 11:41 
AnswerRe: sub menu problem Pinmemberpleykov9-Jun-09 0:30 
QuestionGreat control! PinmemberTilsonR25-Feb-09 5:30 
AnswerRe: Great control! Pinmemberpleykov25-Feb-09 21:29 
GeneralRe: Great control! PinmemberHoneyShyam8-Oct-11 2:45 

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

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

| Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.150327.1 | Last Updated 13 Feb 2009
Article Copyright 2009 by pleykov
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid