Asp.Net Form DefaultButton Error in Firefox(Firefox 中的 Asp.Net 表单 DefaultButton 错误)
问题描述
.Net 为具有DefaultButton"属性集的表单生成的代码包含糟糕的 javascript,它允许该功能在 IE 中运行,但不能在其他浏览器(特别是 Firefox)中运行.
按回车键确实会在所有浏览器中提交表单,但是当它发生在 <textarea> 内时,Firefox 不能忽略按键.控制.结果是多行文本区域控件在 Firefox 中不能是多行的,因为 Enter 键提交表单而不是创建新行.
有关该错误的更多信息,在此处阅读.
这可以在 Asp.Net 3.0+ 中修复,但仍然需要为 2.0 创建解决方法.
关于最轻量级解决方法的任何想法(看起来不像黑客 =D 的黑客)?上面链接中的解决方案让我有点害怕,因为它很容易产生意想不到的副作用.
我使用的这个函数改编自 codesta.
http://blog.codesta.com/codesta_weblog/2007/12/net-gotchas---p.html.
你可以通过像这样用一个 div 包围你的代码来使用它.您可以将表单子类化以自动包含它.我不怎么用它,所以我没有.
<前><div onkeypress="return FireDefaultButton(event, '<%= aspButtonID.ClientID %>')">(你的表格在这里)这是函数.
<前>函数 FireDefaultButton(事件,目标){//srcElement 用于 IEvar element = event.target ||event.srcElement;if (13 == event.keyCode && !(element && "textarea" == element.tagName.toLowerCase())){var defaultButton;defaultButton = document.getElementById(target);if (defaultButton && "undefined" != typeof defaultButton.click){defaultButton.click();event.cancelBubble = true;如果(事件.停止传播)event.stopPropagation();返回假;}}返回真;}The .Net generated code for a form with the "DefaultButton" attribute set contains poor javascript that allows the functionality to work in IE but not in other browsers (Firefox specifcially).
Hitting enter key does submit the form with all browsers but Firefox cannot disregard the key press when it happens inside of a <textarea> control. The result is a multiline text area control that cannot be multiline in Firefox as the enter key submits the form instead of creating a new line.
For more information on the bug, read it here.
This could be fixed in Asp.Net 3.0+ but a workaround still has to be created for 2.0.
Any ideas for the lightest workaround (a hack that doesn't look like a hack =D)? The solution in the above link scares me a little as it could easily have unintended side-effects.
I use this function adapted from codesta. [Edit: the very same one, I see, that scares you! Oops. Can't help you then.]
http://blog.codesta.com/codesta_weblog/2007/12/net-gotchas---p.html.
You use it by surrounding your code with a div like so. You could subclass the Form to include this automatically. I don't use it that much, so I didn't.
<div onkeypress="return FireDefaultButton(event, '<%= aspButtonID.ClientID %>')">
(your form goes here)
</div>
Here's the function.
function FireDefaultButton(event, target)
{
// srcElement is for IE
var element = event.target || event.srcElement;
if (13 == event.keyCode && !(element && "textarea" == element.tagName.toLowerCase()))
{
var defaultButton;
defaultButton = document.getElementById(target);
if (defaultButton && "undefined" != typeof defaultButton.click)
{
defaultButton.click();
event.cancelBubble = true;
if (event.stopPropagation)
event.stopPropagation();
return false;
}
}
return true;
}
这篇关于Firefox 中的 Asp.Net 表单 DefaultButton 错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:Firefox 中的 Asp.Net 表单 DefaultButton 错误
- 良好实践:如何重用 .csproj 和 .sln 文件来为 CI 创建 2022-01-01
- 如何用自己压缩一个 IEnumerable 2022-01-01
- WebMatrix WebSecurity PasswordSalt 2022-01-01
- 输入按键事件处理程序 2022-01-01
- C# 中多线程网络服务器的模式 2022-01-01
- 在哪里可以找到使用中的C#/XML文档注释的好例子? 2022-01-01
- 带有服务/守护程序应用程序的 Microsoft Graph CSharp SDK 和 OneDrive for Business - 配额方面返回 null 2022-01-01
- C#MongoDB使用Builders查找派生对象 2022-09-04
- MoreLinq maxBy vs LINQ max + where 2022-01-01
- Web Api 中的 Swagger .netcore 3.1,使用 swagger UI 设置日期时间格式 2022-01-01
