ASP.NET MenuItem Individual Styles

CodeMonkey1313 picture CodeMonkey1313 · Apr 1, 2009 · Viewed 37k times · Source

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?

Answer

Poustic picture Poustic · Jan 30, 2013

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>