Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to style an asp.net menu with CSS

Tags:

I'm in the process of styling an asp.net menu and I'm trying to understand the meaning of the StaticSelectedStyle-CssClass and StaticHoverStyle-CssClass parameters.

My understanding is that the styles defined with these parameters are applied as CSS classes to the relevant elements, whenever needed. So I created my menu as follows:

<asp:Menu ID="NavigationMenu" DataSourceID="NavigationSiteMapDataSource"          StaticMenuStyle-CssClass="StaticMenuStyle"         StaticMenuItemStyle-CssClass="StaticMenuItemStyle"         StaticSelectedStyle-CssClass="StaticSelectedStyle"         StaticHoverStyle-CssClass="StaticHoverStyle"         Orientation="Horizontal"          MaximumDynamicDisplayLevels="0"          runat="server"> </asp:Menu> 

It works for StaticMenuStyle-CssClass and StaticMenuStyle-CssClass (the classes are applied to the relevant elements), but StaticSelectedStyle-CssClass and StaticHoverStyle-CssClass are not applied, regardless of the selected or hover status of an element.

What am I supposed to do to make this work?

Thanks.

EDIT: Sorry I should have mentioned that this is .NET 4. Here is the generated HTML:

<div id="NavigationMenu">  <ul class="level1 StaticMenuStyle">   <li><a class="level1 StaticMenuItemStyle selected" href="/Link.aspx">Link</a></li>  </ul> </div> 

So as you can see, StaticMenuStyle and StaticMenuItemStyle are applied, but not StaticSelectedStyle-CssClass or StaticHoverStyle-CssClass. Not sure why. I know I can use selected but isn't the expected behavior that StaticSelectedStyle-CssClass be applied??? By using selected I make assumptions as to what .NET does behind the scenes and that's not right.

like image 432
md1337 Avatar asked Apr 16 '10 18:04

md1337


2 Answers

I feel your pain and I wasted all night/morning trying to figure this out. With sheer brute force I figured out a solution. Call it a workaround - but it's simple.

Add the CssClass property to your Menu Control's declaration like so:

<asp:Menu ID="NavigationMenu" DataSourceID="NavigationSiteMapDataSource"         CssClass="SomeMenuClass"         StaticMenuStyle-CssClass="StaticMenuStyle"         StaticMenuItemStyle-CssClass="StaticMenuItemStyle"         Orientation="Horizontal"          MaximumDynamicDisplayLevels="0"          runat="server"> </asp:Menu> 

Just rip out the StaticSelectedStyle-CssClass and StaticHoverStyle-CssClass attributes as they simply don't do jack.

Now create the "SomeMenuClass", doesn't matter what you put in it. It should look something like this:

.SomeMenuClass {     color:Green; } 

Next add the following two CSS Classes:

For "Hover" Styling add:

.SomeMenuClass a.static.highlighted {     color:Red !important; } 

For "Selected" Styling add:

.SomeMenuClass a.static.selected {     color:Blue !important; } 

There, that's it. You're done. Hope this saves some of you the frustration I went through. BTW: You mentioned:

I seem to be the first one to ever report on what seems to be a bug.

You also seemed to think it was a new .NET 4.0 bug. I found this: http://www.velocityreviews.com/forums/t649530-problem-with-staticselectedstyle-and-statichoverstyle.html posted back in 2008 regarding Asp.Net 2.0 . How are we the only 3 people on the planet complaining about this?

How I found the workaround (study the HTML output):

Here is the HTML output when I set StaticHoverStyle-BackColor="Red":

#NavigationMenu a.static.highlighted {     background-color:Red; } 

This is the HTML output when setting StaticSelectedStyle-BackColor="Blue":

#NavigationMenu a.static.selected {     background-color:Blue;     text-decoration:none; } 

Therefore, the logical way to override this markup was to create classes for SomeMenuClass a.static.highlighted and SomeMenuClass a.static.selected

Special Notes:

You MUST also use "!important" on ALL the settings in these classes because the HTML output uses "#NavigationMenu", and that means any styles Asp.Net decides to throw in there for you will have inheritance priority over any other styles for the Menu Control with the ID "NavigationMenu". One thing I struggled with was padding, till I figured out Asp.Net was using "#NavigationMenu" to set the left and right padding to 15em. I tacked on "!important" to my SomeMenuClass styles and it worked.

like image 199
MikeTeeVee Avatar answered Sep 22 '22 13:09

MikeTeeVee


Alright, so there are obviously not a whole lot of people who have tried the .NET 4 menu as of today. Not surprising as the final version was released a couple days ago. I seem to be the first one to ever report on what seems to be a bug. I will report this to MS if I find the time, but given MS track-record of not paying attention to bug reports I'm not rushing this.

Anyway, at this point the least worst solution is to copy and paste the CSS styles generated by the control (check the header) into your own stylesheet and modify it from there. After you're done doing this, don't forget to set IncludeStyleBlock="False" on your menu so as to prevent the automatic generation of the CSS, since we'll be using the copied block from now on. Conceptually this is not correct as your application shouldn't rely on automatically generated code, but that's the only option I can think of.

like image 25
md1337 Avatar answered Sep 22 '22 13:09

md1337