views:

775

answers:

4

I'm working on a web application that uses ASP.NET 3.5 and C#. Structurally, I have a master page with a menu control on it. The control serves as my navigation, and it gets its items from a SiteMapDataSource control and a corresponding Web.sitemap file.

The problem is that some styles do not render properly when you specify the CssClass property. More specifically, the selected and hover styles don't respond to css styles. Consider the code below:

<%@ Master Language="C#" AutoEventWireup="true" CodeFile="Site.master.cs" Inherits="Site" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.or/TR/xhtml1/DTD/xhtml1-transitional.dtd"&gt;

<html xmlns="http://www.w3.org/1999/xhtml"&gt;
<head runat="server">
<title>A webpage</title>
</head>
<body>
<form id="form1" runat="server">
<div id="page">
    <asp:Menu 
        ID="navMenu" 
        Orientation="Horizontal"
        StaticMenuStyle-CssClass="staticMenu"
        StaticMenuItemStyle-CssClass="staticMenuItem"
        StaticSelectedStyle-CssClass="staticSelectedItem"
        StaticHoverStyle-CssClass="staticHoverItem"
        runat="server">
    </asp:Menu>
    <asp:SiteMapDataSource ID="srcSiteMap" runat="server" ShowStartingNode="false" />
    <br />
    <asp:ContentPlaceHolder id="ContentPlaceHolder1" runat="server">
    </asp:ContentPlaceHolder>
</div>
</form>
</body>
</html>

Suppose I had a corresponding .css file with the following:

.staticMenuItem { background-color:Red; }
.staticSelectedItem { background-color:Green; }
.staticHoverItem { background-color:Blue; }

What will happen is that my item backgrounds will properly be red, but my selected item will not be green and the item I'm hovering my mouse over will not be blue. This seems true regardless of whether or not I include the style in the head of the master page or in an external file in default theme as specified in the web.config file.

If I specify the styles in the asp.net xml like so:

<asp:Menu
    ID="navMenu"
    Orientation="Horizontal"
    runat="server">
<StaticSelectedStyle 
    BackColor="Green"
    Font-Underline="True"
    Font-Bold="True" />
<StaticHoverStyle 
    BackColor="Gray" />
</asp:Menu>

It appears to work properly in Firefox, but the style is never embedded in the html in Internet Explorer. Odd.

Does anybody have any insight into what is causing this problem and how to neatly work around it? I'm aware I might be able to programmically determine the current page and select the corresponding menu item manually so it receives the proper style class, but before I resort to hacking C# and Javascript together to fix this functionality, I'm open to ideas.

A: 

Hover does not work in IE6...this may be your problem. It can be fixed with javascript though...http://www.robspangler.com/blog/hover-pseudo-class-bug-fix-for-ie6/

Mike Cellini
Also with your page open in Firefox/ID...check to see that your menu items are getting assigned their proper classes/styles (choose View Page Source). You can go one step further and use Firebug to see what styles are being applied and which ones are being overwritten.
Mike Cellini
Hmm, since asp.net often renders controls with javascript and takes the user's browser into account, this seems like a rather strange oversight, but if there's no other way, it'll have to be done.Also, upon closer inspection, even if I set the StaticSelectedStyle-CssClass property of the Menu control, it's not set properly in the html. Instead, it's just a class called "selected" and applying styles to #navMenu a.selected will, in fact, style the selected page. How inconsistent. Still, I'll hold on to the hope that there's a better way than javascript and inconsistent css.
Shaun
A: 

Hello, I use exactly the same development environment like you, and I could produce a menu which has red items by default, which become blue on hovering and green when selected. I have embedded the theme in the web.config file. I use IE 8. Might it be a problem with the IE version? Greetz

Jan-Frederik Carl
Apparently I was having some trouble with my Visual Studio 2010 configuration; I have since been able to get the styles to render properly in IE8 using the xml preferences using IIS7 instead. I'll have to clean that up. I certainly appreciate you looking into it. However, I still am not able to use external css classes for the selected and hover styles without hacking together something that plays off of Asp.net's naming scheme when rendering the menu control. Did you specify the styles in the xml form, or were you able to get the external stylesheet working?
Shaun
I used the external stylesheet. But of course, I still use VS 2008 and had no problems in this direction ;) Interesting problem.
Jan-Frederik Carl
A: 

Quite odd, when I used the code I provided in a fresh solution, it worked perfectly. Apparently, my original solution was using ASP.NET 4.0, and for whatever reason, copying the code given here didn't work. I tried specifying ASP.NET 3.5 as the compiler in Visual Studio, and 'lo and behold, it worked. When I set ASP.NET 4.0 as the compiler once again, it worked.

Upon closer inspection, I see that transforming the Visual Studio 2010 solution from ASP.NET 3.5 to ASP.NET 4.0 set the controlRenderingCompatibilityVersion property to "true" in the web.config file. This had the effect of causing the menu control to render as a table (as it does in 3.5) instead of a list item (as it does in 4.0). Removing this property broke the page once more, and setting the menu control's RenderingMode property to "table" fixed it.

So, in summary, this bug only appears to exist in ASP.NET 4.0 with menu controls being rendered as lists, at least for me. If anybody else has further insight, I'd be happy to hear it.

Shaun
A: 

In ASP.NEt 4.0 it puts the actual "selected" attribute in the class of the a tag. So what I did was target it with CSS and make sure that I was setting the size of my a tag, padding etc instead of the li and it works like a dream.

Tyrus