ASP.NET TextBox OnTextChanged在Firefox中触发两次

时间:2010-02-04 19:19:10

标签: asp.net firefox textbox

嗨,我正面临一个只在FF中发生的奇怪问题。我有一个带OnTextChanged处理程序的TextBox控件。事件处理程序在大多数情况下工作正常,但是当用户更改文本并按下FF中的Enter时,OnTextChanged事件被调用两次。我在Firebug中观察到第一个请求因第二个事件而被实际取消的问题。

Test.aspx文件

<%@ Page Language="C#" AutoEventWireup="True" CodeFile="~/Test.aspx.cs" Inherits="T.Test" %>

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
        "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html>
    <head>
        <title>Custom TextBox - OnTextChanged - C# Example</title>
    </head>
    <body>
        <form id="Form1" method="post" runat="server">
            <asp:ScriptManager runat="server" ID="SM">
            </asp:ScriptManager>
            <h3>Custom TextBox - OnTextChanged - C# Example</h3>
            <asp:UpdatePanel runat="server" ID="Panel1">
                <ContentTemplate>
                    <asp:Panel runat="server" ID="Panel2">
                        <asp:TextBox ID="TextBox1" AutoPostBack="true" OnTextChanged="OnTextChanged" runat="server">Hello World!
                        </asp:TextBox>
                    </asp:Panel>
                </ContentTemplate>
            </asp:UpdatePanel>
        </form>
    </body>
    </html>

Test.aspx.cs

using System;
using System.Web.UI;

namespace T
{
    public partial class Test : Page
    {       
        protected void OnTextChanged(object sender, EventArgs e)
        {
            var a = 0;
        }

    }
}

设置一个断点@ var a,你将能够看到在更改文本并按下FF(v3.5.7)中的输入后,OnTextChanged事件被调用两次。

所以我的问题是,正确处理OnTextChanged事件的最佳方法是什么,以便在文本框中点击输入不会触发双重回发。

此致

2 个答案:

答案 0 :(得分:2)

我不知道为什么它与FireFox隔离,但是如果删除AutoPostBack属性,那将解决问题。

还有一个解释here为什么它会重新发布两次。

答案 1 :(得分:0)

我知道这是一个旧线程,但可能对其他人有帮助。

我在验证输入的文本时遇到了同样的问题。我触发了 2 个事件,所以我将这个脚本放在页面顶部,这会导致 Enter 只是选项卡到下一个控件,而不是提交表单。文本框保持不变 AutoPostBack="true" OnTextChanged="xxx_TextChanged"

''''
<script type="text/javascript">
   $('body').on('keydown', 'input, select', function (e) {
   if (e.key === "Enter") {
   var self = $(this), form = self.parents('form:eq(0)'), focusable, next;
   focusable = form.find('input,a,select,button,textarea').filter(':visible');
   next = focusable.eq(focusable.index(this) + 1);
   if (next.length) {
   next.focus();
   } else {
   form.submit();
   }
   return false;
   }
    });
</script>
''''
          
相关问题