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

| Share

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
 

Mark perkins said:

For me, I added this code to every masterpage file in my site. The reason i added it in the AddedControl method was so that i didn't have to use a basepage or parent class that every single aspx file had to inherit. And its the only method that i found to run before the Page_PreInit. Try it! hope it works for you!

  protected override void AddedControl(Control control, int index)

   {

       if (Request.ServerVariables["http_user_agent"].IndexOf("Safari", StringComparison.CurrentCultureIgnoreCase) != -1)

           this.Page.ClientTarget = "uplevel";

       base.AddedControl(control, index);

   }

January 6, 2009 12:52 PM
 

amit said:

Hi,

I have been asp.net menu in one of my control (head.ascx) when i use this code in my page. It work fine.But it doesn't work in my control.. how can i use this code in my control. emailID(amit.thakur@aimsdigital.com)

January 14, 2009 1:31 AM
 

Matt Williamson said:

Just a guess, but add the code to your user control's Init Event ... msdn.microsoft.com/.../system.web.ui.usercontrol_members.aspx

January 14, 2009 12:59 PM
 

rui said:

Thanks, works perfect

February 27, 2009 1:40 PM
 

mr. burns said:

excelent!

February 27, 2009 8:29 PM
 

Scott said:

This is the best fix I've come across this issue.

March 6, 2009 1:42 PM
 

Sam said:

Worked great !  Thank you.  Would not have figured this one out.

March 19, 2009 11:01 PM
 

Jeff said:

Thank you.  I added Mark Perkins code to my master page and it works great in both Safari and Chrome.

April 5, 2009 8:40 PM
 

Stefano said:

Great!

I added Matt Williamson alternate fix in the Page_Init event of my user control containing an Asp Menu and it works great in both Safari and Chrome.

I hope someone can enjoy the VB version of the code:

Protected Sub Page_Init(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Init

       Try

           If Request.UserAgent.Contains("AppleWebKit") Then

               Request.Browser.Adapters.Clear()

           End If

       Catch ex As Exception

       End Try

   End Sub

April 8, 2009 4:15 AM
 

Adam said:

Have a beginers question.

How exactly do I emplement this in a vb project?

can you explain step by step?

thanks :)

May 19, 2009 3:41 AM
 

Matt Williamson said:

Add that method to the code behind of your page.

May 19, 2009 10:43 AM
 

O said:

Optional way to base page or in user controls is using the global.asax. This can be a quick fix.

vb.net

Sub Application_BeginRequest(ByVal sender As Object, ByVal e As EventArgs)

       Try

           If Request.UserAgent.Contains("AppleWebKit") Then

               Request.Browser.Adapters.Clear()

           End If

       Catch ex As Exception

       End Try

   End Sub

June 2, 2009 2:16 PM
 

O said:

Thank you very much for the post. It helped me a lot.

June 2, 2009 2:17 PM
 

mcg987 said:

I added Mark Perkins (January 6, 2009 12:52 PM) solution from above and it work without any issues.

Thank you

June 11, 2009 3:47 AM
Powered by Community Server (Non-Commercial Edition), by Telligent Systems