Click here to Skip to main content
13,860,597 members
Click here to Skip to main content
Add your own
alternative version


32 bookmarked
Posted 13 Feb 2009
Licenced CPOL

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

, 13 Feb 2009
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

You may also be interested in...


Comments and Discussions

QuestionMouseover Tabs Menu Pin
MS Babu10-May-13 20:49
memberMS Babu10-May-13 20:49 
QuestionNeed Help please? Pin
metharam6-Feb-13 6:02
membermetharam6-Feb-13 6:02 
GeneralMy vote of 4 Pin
Shyam Raghuvanshi8-Oct-11 1:58
memberShyam Raghuvanshi8-Oct-11 1:58 
GeneralRestriction on Menu Pin
rockygorden1-Mar-10 3:43
memberrockygorden1-Mar-10 3:43 
GeneralRe: Restriction on Menu Pin
pleykov1-Mar-10 4:32
memberpleykov1-Mar-10 4:32 
QuestionInstructions? Pin
JBeckton4-Feb-10 15:20
memberJBeckton4-Feb-10 15:20 
AnswerRe: Instructions? Pin
pleykov8-Feb-10 2:01
memberpleykov8-Feb-10 2:01 
Generalsub menu problem Pin
Dustin Hlava8-Jun-09 11:41
memberDustin Hlava8-Jun-09 11:41 
AnswerRe: sub menu problem Pin
pleykov9-Jun-09 0:30
memberpleykov9-Jun-09 0:30 
QuestionGreat control! Pin
Trebor7425-Feb-09 5:30
memberTrebor7425-Feb-09 5:30 
AnswerRe: Great control! Pin
pleykov25-Feb-09 21:29
memberpleykov25-Feb-09 21:29 
GeneralRe: Great control! Pin
Shyam Raghuvanshi8-Oct-11 2:45
memberShyam Raghuvanshi8-Oct-11 2:45 

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 | Cookies | Terms of Use | Mobile
Web05 | 2.8.190214.1 | Last Updated 13 Feb 2009
Article Copyright 2009 by pleykov
Everything else Copyright © CodeProject, 1999-2019
Layout: fixed | fluid