views:

306

answers:

1

Hi I'm facing a weird problem that only happens in FF. I have a TextBox control with OnTextChanged handler. The event handler is working fine most of the time, but when the user changed the text and press Enter in FF, the OnTextChanged event is called twice. I observed the problem in Firebug that the first request is actually canceled because of the second event.

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"&gt;
    <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;
        }

    }
}

Put a break point @ var a, and you'll be able to see that after changing text and press enter in FF (v3.5.7), the OnTextChanged event is invoked twice.

So my question is, what's the best way to properly handle OnTextChanged event so that hitting enter in the textbox doesn't trigger double postback.

Regards,

A: 

Hi,

I don't know why it's isolated to FireFox, but if you remove the AutoPostBack property, that will solve the problem.

There is also an explanation here of why it's posting back twice.

keyboardP
Yeah I thought about that, but the problem is now when user finishes typing, the OnTextChanged event doesn't get fired.
BlueFox
Ah, I see. I was testing the code you posted, which works fine with AutoPostBack set to false. However, if you have more than one textbox, the issue comes back.
keyboardP