BUG: TextBox Controls with the AutoPostBack Property Set to True May Call the TextChanged Event Two Times (828979)



The information in this article applies to:

  • Microsoft ASP.NET (included with the .NET Framework) 1.0
  • Microsoft ASP.NET (included with the .NET Framework 1.1)

SYMPTOMS

A TextBox control and a Button control are on a Microsoft ASP.NET Web form and the AutoPostBack event of the TextBox is set to true. When you click the Button control after you type text in the TextBox control, the TextChanged event is triggered two times. You may also notice that the behavior of actions that trigger the TextChanged and the Button_Clicked events is not consistent.

WORKAROUND

To work around this problem, use a script to trap the keypress event and to call the ButtonClick event when the keypress event value is enter.

To do this, follow these steps:
  1. Add the following script block to the <header> section of the WebForm1.aspx code:
    <script language="jscript">
          function clickButton() 
          {     
           if (event.keyCode == 13) 
           {      
             myform.Button1.click();
             return false;
           }
         }
    </script>
  2. Add the onkeypress event for the TextBox control as follows:
    <form id="myform" method="post" runat="server">
     <asp:TextBox id="TextBox1" onkeypress="return(clickButton());" 
    ontextchanged="TextBox1_TextChanged" runat="server" AutoPostBack="True"></asp:TextBox> <asp:Button id="Button1" onclick="Button1_Clicked" runat="server" Text="Button"></asp:Button> </form>

STATUS

Microsoft has confirmed that this is a bug in the Microsoft products that are listed at the beginning of this article.

MORE INFORMATION

Steps to Reproduce the Behavior

  1. Start Microsoft Visual Studio .NET.
  2. Use Microsoft Visual C# .NET or Microsoft Visual Basic .NET to create a new ASP.NET Web Application project. By default, WebForm1.aspx is created.
  3. In the Design view, right-click WebForm1.aspx, and then click View HTML Source.
  4. To add a Button control and a TextBox control with AutoPostBack event, replace the existing code in WebForm1.aspx with the following code:

    Visual C# .NET Code
    <%@ Page language="c#" trace=false debug=true %>
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" >
    <HTML>
       <HEAD>
          <title>WebForm1</title>
          <script runat="server">  
    
    	private void Page_Load(object sender, System.EventArgs e)
    	{		
             if(!Page.IsPostBack)
             {
                Session["tc"]=0;
                Session["bc"]=0;
             }
    	}
    
    	private void TextBox1_TextChanged(object sender, System.EventArgs e)
    	{
             Session["tc"] = Int32.Parse(Session["tc"].ToString()) + 1 ;
             Response.Write("<br>Text change times : "+  Session["tc"]);  
    	}
    
    	private void Button1_Clicked(object sender, System.EventArgs e)
    	{
    	      Session["bc"] = Int32.Parse(Session["bc"].ToString()) + 1 ;
           Response.Write("<br>Button click times : "+  Session["bc"]);        
    	}
    </script>
    </HEAD>
    <body MS_POSITIONING="GridLayout">
     <form id="myform" method="post" runat="server">
          <asp:TextBox id="TextBox1"  ontextchanged="TextBox1_TextChanged" runat="server" AutoPostBack="True"></asp:TextBox>
          <asp:Button id="Button1" onclick="Button1_Clicked" runat="server" Text="Button"></asp:Button>
     </form>
       </body>
    </HTML>
    Visual Basic .NET Code
    <%@ Page language="vb" trace=false debug=true %>
    <HTML>
       <HEAD>
          <title>WebForm1</title>      
          <script runat=server>
          
       Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
          If Not Page.IsPostBack Then
             Session("tc") = 0
             Session("bc") = 0
          End If
       End Sub
    
       Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) 
          Session("bc") = (Int32.Parse(Session("bc").ToString()) + 1).ToString()
          Response.Write("<br>Button click times : " + Session("bc"))
       End Sub
    
       Private Sub TextBox1_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) 
          Session("tc") = (Int32.Parse(Session("tc").ToString()) + 1).ToString()
          Response.Write("<br>Text change times : " + Session("tc"))
       End Sub
          </script> 
       </HEAD>
       <body MS_POSITIONING="GridLayout">
        <form id="myform" method="post" runat="server">
             <asp:TextBox id="TextBox1"  OnTextChanged=TextBox1_TextChanged style="Z-INDEX: 101; LEFT: 251px; POSITION: absolute; TOP: 144px" runat="server" AutoPostBack="True"></asp:TextBox>
             <asp:Button id="Button1" OnClick=Button1_Click style="Z-INDEX: 102; LEFT: 298px; POSITION: absolute; TOP: 207px" runat="server" Text="Button"></asp:Button>
          </form>
       </body>
    </HTML>

  5. On the Debug menu, click Start to run the application.
  6. In the box, type test, and then click Button.
  7. You may notice that the TextChanged event is called two times and that the Button_Clicked event is called only one time.

REFERENCES

For more information , visit the following Microsoft Developer Network (MSDN) Web site:

Modification Type:MajorLast Reviewed:10/6/2003
Keywords:kbpending kbServerControls kbScript kbWebForms kbbug KB828979 kbAudDeveloper