Click here to Skip to main content
15,885,366 members
Articles / Web Development / ASP.NET

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

Rate me:
Please Sign up or sign in to vote.
4.40/5 (3 votes)
13 Feb 2009CPOL1 min read 45.7K   1.1K   32   12
Explains how you can dynamically generate a Superfish menu.

DynamicFishMenu

Introduction

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?

Background

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:

ASP.NET
<%@ Control Language="C#"
            AutoEventWireup="true"
            CodeBehind="XmlMenu.ascx.cs"
            Inherits="DynamicFishMenu.controls.XmlMenu" %>

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

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:

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

string GetMapPath(SiteMapMenus menu)
{
    string serverMapPath = Server.MapPath("~");
    string path = String.Empty;
    switch (menu)
    {
        case SiteMapMenus.MenuAdmin:
            path = serverMapPath + @"\maps\MenuAdmin.sitemap";
            break;
        case SiteMapMenus.MenuAcc:
            path = serverMapPath + @"\maps\MenuAcc.sitemap";
            break;
        case SiteMapMenus.MenuSimple:
            path = serverMapPath + @"\maps\MenuSimple.sitemap";
            break;
        default:
            break;
    }
    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.

C#
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>");
    }
    else
    {
        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:

C#
protected void mainMenu_PreRender(object sender, EventArgs e)
{
    mainMenu.InitMenuDataSource(SiteMapMenus.MenuAdmin);
    // comment above line and uncomment below to switch menus
    //mainMenu.InitMenuDataSource(SiteMapMenus.MenuAcc);
}

Conclusion

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

Happy coding!

History

  • 13 Feb 2009 - First post.

License

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


Written By
Web Developer Sirma Solutions
Bulgaria Bulgaria
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
QuestionMouseover Tabs Menu Pin
MS Babu10-May-13 19:49
MS Babu10-May-13 19:49 
QuestionNeed Help please? Pin
metharam6-Feb-13 5:02
metharam6-Feb-13 5:02 
GeneralMy vote of 4 Pin
Shyam Raghuvanshi8-Oct-11 0:58
Shyam Raghuvanshi8-Oct-11 0:58 
GeneralRestriction on Menu Pin
rockygorden1-Mar-10 2:43
rockygorden1-Mar-10 2:43 
GeneralRe: Restriction on Menu Pin
pleykov1-Mar-10 3:32
pleykov1-Mar-10 3:32 
QuestionInstructions? Pin
JBeckton4-Feb-10 14:20
JBeckton4-Feb-10 14:20 
AnswerRe: Instructions? Pin
pleykov8-Feb-10 1:01
pleykov8-Feb-10 1:01 
Generalsub menu problem Pin
Dustin Hlava8-Jun-09 10:41
Dustin Hlava8-Jun-09 10:41 
AnswerRe: sub menu problem Pin
pleykov8-Jun-09 23:30
pleykov8-Jun-09 23:30 
QuestionGreat control! Pin
Trebor7425-Feb-09 4:30
Trebor7425-Feb-09 4:30 
AnswerRe: Great control! Pin
pleykov25-Feb-09 20:29
pleykov25-Feb-09 20:29 
GeneralRe: Great control! Pin
Shyam Raghuvanshi8-Oct-11 1:45
Shyam Raghuvanshi8-Oct-11 1: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.