ASP.NET MenuItem Individual Styles

I’m hoping to use an ASP.NET Menu Control for navigation through my site. However, I’ve got a requirement that each MenuItem must be styled differently (different colors, both static, and onHover). Without creating a custom class that would inherit from MenuItem, is this possible?

Thoughts on a better solution?


Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.

Method 1

If anyone else bumps into the same question…

A quick and dirty method that worked for me is to force HTML contents into the MenuItem Text (with appropriate escaping). You can then style it any way you want in your CSS, or even set each menu item to use a different style:

<asp:MenuItem Text="<span class=&quot;menuitem_text&quot;>Text Here</span>" />

The HTML ends up inside the <a> tag:
<li><a  ...><span class="menuitem_text">Text Here</span></a></li>

Method 2

Short of overriding RenderContents on Menu, your options are very limited. Most of what you’d need is private and sealed and you won’t get anywhere there.

My solution would be to use templates. You could use MenuItem.Value or Depth and and ItemIndex to identify each item and provide necessary attributes.

In Page:

<asp:Menu ID="menu" runat="server" DynamicHorizontalOffset="2" StaticSubMenuIndent="10px">
        <asp:MenuItem Text="Item 1" Value="value 1">
            <asp:MenuItem Text="Item 2" Value="value 2">
                <asp:MenuItem Text="Item 3" Value="value 3"></asp:MenuItem>
            <asp:MenuItem Text="Item 4" Value="value 4">
                <asp:MenuItem Text="Item 5" Value="value 5"></asp:MenuItem>
            <asp:MenuItem Text="Item 6" Value="value 6"></asp:MenuItem>
        <asp:MenuItem Text="Item 7" Value="value 7"></asp:MenuItem>
        <asp:MenuItem Text="Item 8" Value="value 8"></asp:MenuItem>
        <asp:Panel runat="server" ForeColor='<%# GetItemColor(Container) %>'>
            <%# Eval("Text") %> - <%# Eval("Value") %>
        <asp:Panel ID="Panel1" runat="server" ForeColor='<%# GetItemColor(Container) %>'>
            <%# Eval("Text") %> - <%# Eval("Value") %>

In Code (never mind silliness of this code, it is just to demonstrate the principle):
public Color GetItemColor(MenuItemTemplateContainer container)
    MenuItem item = (MenuItem)container.DataItem;

    //identify based value
    if (item.Value == "value 2")
        return Color.Brown;

    //identify based on depth and index
    if (item.Depth == 0)
        switch (container.ItemIndex)
            case 0: return Color.Red;
            case 1: return Color.Blue;
            case 2: return Color.DarkGreen;
                return Color.Black;
        switch (container.ItemIndex)
            case 0: return Color.Purple;
            case 1: return Color.Aqua;
            case 2: return Color.DarkOrange;
                return Color.Black;

Method 3

Try like this to set style for each menu item:

Code behind:

 mnuMail.Items[1].Text = "<span class="bold">Inbox</span>";

CSS class:
   font-weight: bold;

Method 4

If you generate the menu programmatically, you can add the style and onmouseover/onmouseout attributes when creating each MenuItem, e.g.:

menuItem.Attributes["style"] = "color: red;";
menuItem.Attributes["onmouseover"] = "javascript:Highlight(this);";

Alternatively, try adding those attributes in the markup, IntelliSense won’t tell you that they work, but they usually do (I haven’t tested it specifically with MenuItems):
<asp:menuitem navigateurl="Home.aspx" 
    style="color: red;" onmouseover="Highlight(this);" onmouseout="Unhighlight(this);"/>

You might have some luck with CSS Friendly Adapters.

Of course you can create an inherited class and re-work the rendering routines…

All methods was sourced from or, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

0 0 votes
Article Rating
Notify of

Inline Feedbacks
View all comments
Would love your thoughts, please comment.x