asp.net 菜单控件在 Safari 中无法正确呈现
我正在开发的网站正在使用 Databound asp:Menu 控件。 当发送 1 个菜单项时,它会呈现在 Firefox(和 IE)中绝对正确的 HTML,但在 Safari 和 Chrome 中确实搞乱了代码。 以下是发送到每个浏览器的代码。 我已经测试了几个浏览器,它们的渲染效果都非常相似,所以我只发布渲染源的两个变体。
我的问题是:如何让 ASP.NET 向 Chrome 和 Safari 发送与 Firefox 和 IE 相同的 html 和 javascript?
<!-- This is how the menu control is defined -->
<asp:Menu ID="menu" runat="server" BackColor="#cccccc"
DynamicHorizontalOffset="2" Font-Names="Verdana" StaticSubMenuIndent="10px" StaticDisplayLevels="1"
CssClass="left_menuTxt1" Font-Bold="true" ForeColor="#0066CC">
<DataBindings>
<asp:MenuItemBinding DataMember="MenuItem" NavigateUrlField="NavigateUrl" TextField="Text"
ToolTipField="ToolTip" />
</DataBindings>
<StaticSelectedStyle BackColor="#0066CC" HorizontalPadding="5px" VerticalPadding="2px"
Font-Names="Verdama" CssClass="left_menuTxt1" Font-Bold="true" />
<StaticMenuItemStyle HorizontalPadding="5px" VerticalPadding="8px" />
<DynamicMenuStyle BackColor="#fbfbfb" BorderColor="#989595" BorderStyle="Inset" BorderWidth="1"
Width="80px" VerticalPadding="1" />
<DynamicMenuItemStyle HorizontalPadding="5px" VerticalPadding="2px" Font-Name="Verdama"
ForeColor="#c6c4c4" CssClass="left_menuTxt1" Font-Bold="true" />
<DynamicSelectedStyle BackColor="#cccccc" HorizontalPadding="5px" VerticalPadding="2px"
Font-Names="Verdama" CssClass="left_menuTxt1" Font-Bold="true" />
</asp:Menu>
<!-- From Safari View Page Source (Chrome source very similar) -->
<span title="Order" class="ctl00_leftNav_menu_4">
<a class="ctl00_leftNav_menu_1 ctl00_leftNav_menu_3"
href="javascript:__doPostBack('ctl00$leftNav$menu','oMy Order')">
My Order
<img src="/WWW/WebResource.axd?d=glUTEfEv7p9OrdeaMxkMzhqz2JugrMr8aE43O2XGHAA1&t=633590571537099818"
alt="Expand My Order"
align="absmiddle"
style="border-width:0px;" /></a></span><br />
<!-- From Firefox View Page Source (IE View page similar) -->
<table>
<tr onmouseover="Menu_HoverStatic(this)"
onmouseout="Menu_Unhover(this)"
onkeyup="Menu_Key(event)"
title="Order"
id="ctl00_leftNav_menun0">
<td>
<table class="ctl00_leftNav_menu_4" cellpadding="0" cellspacing="0" border="0" width="100%">
<tr>
<td style="white-space:nowrap;width:100%;">
<a class="ctl00_leftNav_menu_1 ctl00_leftNav_menu_3"
href="../Order/OrderList.aspx">
My Order
</a>
</td>
<td style="width:0;">
<img src="/WWW/WebResource.axd?d=glUTEfEv7p9OrdeaMxkMzhqz2JugrMr8aE43O2XGHAA1&t=633590571537099818"
alt="Expand My Order" style="border-style:none;vertical-align:middle;" />
</td>
</tr>
</table>
</td>
</tr>
</table>
更新:我的解决方案帖子是正确的..但我可以'不要将我自己的标记为正确...所以如果有人想复制它,我可以关闭它。 :)
The site I'm working on is using a Databound asp:Menu control. When sending 1 menu item it renders HTML that is absolutely correct in Firefox (and IE), but really messed up code in Safari and Chrome. Below is the code that was sent to each browser. I've tested it a few browsers, and they are all pretty similarly rendered, so I am only posting the two variations on the rendering source.
My question is: How do I get ASP.NET to send the same html and javascript to Chrome and Safari as it does to Firefox and IE?
<!-- This is how the menu control is defined -->
<asp:Menu ID="menu" runat="server" BackColor="#cccccc"
DynamicHorizontalOffset="2" Font-Names="Verdana" StaticSubMenuIndent="10px" StaticDisplayLevels="1"
CssClass="left_menuTxt1" Font-Bold="true" ForeColor="#0066CC">
<DataBindings>
<asp:MenuItemBinding DataMember="MenuItem" NavigateUrlField="NavigateUrl" TextField="Text"
ToolTipField="ToolTip" />
</DataBindings>
<StaticSelectedStyle BackColor="#0066CC" HorizontalPadding="5px" VerticalPadding="2px"
Font-Names="Verdama" CssClass="left_menuTxt1" Font-Bold="true" />
<StaticMenuItemStyle HorizontalPadding="5px" VerticalPadding="8px" />
<DynamicMenuStyle BackColor="#fbfbfb" BorderColor="#989595" BorderStyle="Inset" BorderWidth="1"
Width="80px" VerticalPadding="1" />
<DynamicMenuItemStyle HorizontalPadding="5px" VerticalPadding="2px" Font-Name="Verdama"
ForeColor="#c6c4c4" CssClass="left_menuTxt1" Font-Bold="true" />
<DynamicSelectedStyle BackColor="#cccccc" HorizontalPadding="5px" VerticalPadding="2px"
Font-Names="Verdama" CssClass="left_menuTxt1" Font-Bold="true" />
</asp:Menu>
<!-- From Safari View Page Source (Chrome source very similar) -->
<span title="Order" class="ctl00_leftNav_menu_4">
<a class="ctl00_leftNav_menu_1 ctl00_leftNav_menu_3"
href="javascript:__doPostBack('ctl00$leftNav$menu','oMy Order')">
My Order
<img src="/WWW/WebResource.axd?d=glUTEfEv7p9OrdeaMxkMzhqz2JugrMr8aE43O2XGHAA1&t=633590571537099818"
alt="Expand My Order"
align="absmiddle"
style="border-width:0px;" /></a></span><br />
<!-- From Firefox View Page Source (IE View page similar) -->
<table>
<tr onmouseover="Menu_HoverStatic(this)"
onmouseout="Menu_Unhover(this)"
onkeyup="Menu_Key(event)"
title="Order"
id="ctl00_leftNav_menun0">
<td>
<table class="ctl00_leftNav_menu_4" cellpadding="0" cellspacing="0" border="0" width="100%">
<tr>
<td style="white-space:nowrap;width:100%;">
<a class="ctl00_leftNav_menu_1 ctl00_leftNav_menu_3"
href="../Order/OrderList.aspx">
My Order
</a>
</td>
<td style="width:0;">
<img src="/WWW/WebResource.axd?d=glUTEfEv7p9OrdeaMxkMzhqz2JugrMr8aE43O2XGHAA1&t=633590571537099818"
alt="Expand My Order" style="border-style:none;vertical-align:middle;" />
</td>
</tr>
</table>
</td>
</tr>
</table>
Update: My solution post is correct.. but i can't mark my own as correct... so if anyone wants to copy it so I can close this. :)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(9)
我从 weblogs.asp.net。
这可能是一个黑客行为,但它确实有效。
这种跨浏览器兼容性的斗争正在变得令人不安。
如果有人有更好的解决方案,而不是黑客,如果您发布它,我将不胜感激。 从我广泛的网络搜索来看,似乎我并不是唯一一个遇到菜单控件问题的人,因此一些好的参考资料可以帮助其他处于相同情况的人。
I found this solution from a comment on weblogs.asp.net.
It might be a hack, but it does work.
This cross browser compatibility struggle is getting upsetting.
If anyone has a better solution that's not so much a hack, I would be grateful if you posted it. And from my extensive web searches, it looks like I'm not alone with this problem with the menu control, so some good references would help out others in the same situation.
我在使用 asp:menu 控件和 webkit 时也遇到过问题。 另外,很难完全按照我想要的方式设计。 我的建议是使用 CSS 友好的控制适配器:
这个将菜单表格转换为更现代且 SEO 友好的标记。 您的菜单将看起来更像这样:
在我的测试中,所有浏览器中的标记都是相同的。
I've had problems with the asp:menu control and webkit as well. Plus it's hard to style exactly the way I want. My recommendation is to use the CSS Friendly Control Adapters:
This converts the menu's table into much more modern and SEO-friendly markup. Your menu will look more like this:
In my testing, the markup is the same in all browsers.
如果您有多个 Web 应用程序,以下是解决 chrome 和 safari 问题的最简单方法:
在“%SystemRoot%\Microsoft.NET\Framework[version]\CONFIG\Browsers”中创建一个名为 safari.browser 的文件,该文件包含以下内容:
这将告诉 asp.net 在呈现 safari 的菜单控件时不要使用适配器。
Safari1Plus 是在同一目录中的 mozilla.browser 文件末尾定义的。 这也适用于 Chrome,因为它们都使用 webkit,这就是 ASP.NET 识别 Safari1Plus 的方式。
接下来运行 %SystemRoot%\Microsoft.NET\Framework[version]\aspnet_regbrowsers -i
这会将所有浏览器文件编译成程序集并将其添加到 GAC 中。
现在,asp.net 菜单将在 safari 和 chrome 中正确呈现。
或者,您可以将该文件添加到每个 Web 应用程序的 App_Browsers 目录中。
Here's the easiest way to fix this issue for both chrome and safari if you have multiple web apps:
Create a file named safari.browser in "%SystemRoot%\Microsoft.NET\Framework[version]\CONFIG\Browsers" that contains the following:
This will tell asp.net not to use an adapter when rendering the menu control for safari.
Safari1Plus is defined at the end of the mozilla.browser file in the same directory. This works for chrome as well because they both use webkit which is how asp.net identifies Safari1Plus.
Next run %SystemRoot%\Microsoft.NET\Framework[version]\aspnet_regbrowsers -i
this will compile all the browser files into an assembly and add it to the GAC.
now the asp.net menu will render correctly in safari and chrome.
Alternatively you can add the file the the App_Browsers directory in each of your web apps.
我只是想提交一个替代选项。 这适用于 ASP.NET 3.5。
在浏览器文件中,在
标记之间添加以下代码:<浏览器 id="Chrome"parentID="Safari1Plus">
这应该在 Chrome/Safari 中正确呈现菜单控件。
I just wanted to submit an alternate option. This works for ASP.NET 3.5.
In the browser file, add the following code between the
<browsers>
tag:<browser id="Chrome" parentID="Safari1Plus">
<controlAdapters>
<adapter controlType="System.Web.UI.WebControls.Menu" adapterType="" />
</controlAdapters>
</browser>
That should properly render the menu control in Chrome/Safari.
就像魔术一样!
Works like magic!
Mayank Sharma 找到了一种适用于母版页的解决方案,而不是编辑单个页面。 所有使用母版页的页面都被无缝修复。 这仍然是一个黑客,但你做了你必须做的事。 这是一个简单的示例母版页代码。
Mayank Sharma found a solution that works with master pages, rather than editing individual pages. All pages that use the master page are fixed seamlessly. It's still a hack, but you do what you have to. Here's a barebones example master page code behind.
这是转换为 VB.NET 的 @Mayank Sharma / @jball C# 版本。 谢谢你们的修复,困扰我几个月了。 我的问题是除了 IE8 和 Chrome 之外,MAC 和 PC 上的所有浏览器都可以正常工作。 但是,尽管我很喜欢 Chrome,但它经常无法运行 Google 文档 - 请解决这个问题!
您会注意到我必须检查“fake_user_agent”,而不是“Safari”。
Here's the @Mayank Sharma / @jball C# version converted to VB.NET. Thanks for the fix guys, been bugging me for months. My problem was every browser on MAC and PC worked except IE8 and Chrome. But then Chrome, much as I like it, often fails to run Google Docs - work that out!!!
You'll note that I had to check for "fake_user_agent", not "Safari".
Chrome 和 Safari 无法正确渲染菜单控件的问题是由于 Chrome 和 Safari 渲染了导航跳过链接图像框。
如果您在图像上为跳过链接执行 css
display: none;
,则菜单控件将按其应有的方式定位自身。我已经在简单的一级菜单而不是嵌套菜单上对此进行了测试。
http:// www.stfu.com/2011/05/05/asp-net-menu-control-positioning-in-safari-google-chrome/
The issue with Chrome and Safari not rendering the menu control properly is due to the navigation skiplink image box being rendered by Chrome and Safari.
If you do a css
display: none;
on the image for the skiplink then the menu control positions itself like it should.I've tested this on a simple 1 level menu and not nested menus.
http://www.s-t-f-u.com/2011/05/05/asp-net-menu-control-positioning-in-safari-google-chrome/
像这样将
ClientTarget="uplevel"
添加到 page 指令中可以使 Safari 正常工作:Adding
ClientTarget="uplevel"
to the page directive like so makes Safari work: