in

Bunker Hollow

Matt Williamson's home on the web, welcome.

Matt Williamson’s Blog

Personal discoveries of an IT professional.

ASP.NET Menu Control Problem in Safari and Chrome

The ASP.NET Menu controls in my application display correctly in all browsers but Apple's Safari and Google's Chrome.  After researching the problem this afternoon, I'm documenting the fix I used.

ASP.NET Menu Control Renders Correctly in IE, Firefox, and Opera

ASP.NET Menu Control Renders Incorrectly in Safari and Chrome

The Fix:

  1. Create the following PageBase class which inherits System.Web.UI.Page and define a Page_PreInit method which contains the fix:

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;
    namespace MyProject.WebSite
    {
        public class PageBase : System.Web.UI.Page
        {
            protected void Page_PreInit(object sender, EventArgs e)
            {
                // This is necessary because Safari and Chrome browsers don't display the Menu control correctly.
                // All webpages displaying an ASP.NET menu control must inherit this class.
                if (Request.ServerVariables["http_user_agent"].IndexOf("Safari", StringComparison.CurrentCultureIgnoreCase) != -1)
                    Page.ClientTarget = "uplevel";
            }
        }
    }

  2. Change ALL of your webpages to inherit from your new PageBase class instead of the usual Page class.  It's easiest to do a global replace of ": System.Web.UI.Page" with ": PageBase".  Unfortunately, you can't just add the fix to your Master Page, it doesn't contain a PreInit method as it's a web control not a web page.

Alternate Fix:

  1. if (Request.UserAgent.Contains("AppleWebKit")) Request.Browser.Adapters.Clear();
  2. I'm not sure which is "better", but I used the first solution above and it works for me.

References

  1. http://forums.asp.net/t/941229.aspx?PageIndex=1
  2. http://weblogs.asp.net/dannychen/archive/2005/11/21/using-device-filters-and-making-menu-work-with-safari.aspx

Comments

 

ddesi16127 said:

Thanks for this fix!  I was having trouble with my sub-menu items in Opera.  I inserted this code into my basePage and all is fixed.  Thanks again!

October 16, 2008 7:39 PM
 

WotlkPowerLeveling.com said:

Thank you very much, this fix also work for me. Is it possible to have a declarative way which only require Web.config ?

October 25, 2008 11:51 AM
 

VND - Vu Nguyen Learn On The Go said:

Get ASP.NET Menu work on Chrome, Safari and Opera

October 25, 2008 12:01 PM

Leave a Comment

(required)  
(optional)
(required)  
Add
Powered by Community Server (Non-Commercial Edition), by Telligent Systems