Firefox 中的 Asp.Net 表单 DefaultButton 错误 [英] Asp.Net Form DefaultButton Error in Firefox

查看:28
本文介绍了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 错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆