Sunday, 20 February 2011

fixing asp:menu for google chrome asp.net

As some of you using asp.net might notice, Google Chrome has a quirk, it likes simplifying the html code. it isn't very noticeable, most of you will never notice, but when you use the control, the css just doesn't seem to work right for Chrome, the fix for this is to add the following code to your base.master code, or if you dont have a master, to the page's code.

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);
}

In case you are wondering why i use the safari as the user agent, that is because Safari and Chrome use the same rendering engine, the WebKit, thus making this fix good for both browsers.
In case you are using Safari and annoyed about only talking about Chrome, don't be insulted that i only talk about Google's Chrome, after all, chrome is 5 times more poplar (Chrome's market share is at around 13% of all browsers usage, while Safari is only at around 2%)

No comments:

Post a Comment