Microsoft KB Archive/828979

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

Article ID: 828979

Article Last Modified on 5/17/2007

-

APPLIES TO


 * Microsoft ASP.NET 1.0
 * Microsoft ASP.NET 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:   Add the following script block to the section of the WebForm1.aspx code:  function clickButton {           if (event.keyCode == 13) {              myform.Button1.click; return false; }    }   Add the onkeypress event for the TextBox control as follows:    



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

<div class="moreinformation_section">

Steps to Reproduce the Behavior
 Start Microsoft Visual Studio .NET.</li> 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.</li> In the Design view, right-click WebForm1.aspx, and then click View HTML Source.</li>  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=&quot;c#&quot; trace=false debug=true %> <!DOCTYPE HTML PUBLIC &quot;-//W3C//DTD HTML 4.0 Transitional//EN&quot; > <HTML> <HEAD> WebForm1 <script runat=&quot;server&quot;>

private void Page_Load(object sender, System.EventArgs e)   { if(!Page.IsPostBack) {           Session[&quot;tc&quot;]=0; Session[&quot;bc&quot;]=0; }   }

private void TextBox1_TextChanged(object sender, System.EventArgs e)   { Session[&quot;tc&quot;] = Int32.Parse(Session[&quot;tc&quot;].ToString) + 1 ; Response.Write(&quot; Text change times : &quot;+ Session[&quot;tc&quot;]); }

private void Button1_Clicked(object sender, System.EventArgs e)   { Session[&quot;bc&quot;] = Int32.Parse(Session[&quot;bc&quot;].ToString) + 1 ; Response.Write(&quot; Button click times : &quot;+ Session[&quot;bc&quot;]); } </HEAD> <body MS_POSITIONING=&quot;GridLayout&quot;> <form id=&quot;myform&quot; method=&quot;post&quot; runat=&quot;server&quot;> </asp:TextBox> <asp:Button id=&quot;Button1&quot; onclick=&quot;Button1_Clicked&quot; runat=&quot;server&quot; Text=&quot;Button&quot;></asp:Button> </HTML> Visual Basic .NET Code

<%@ Page language=&quot;vb&quot; trace=false debug=true %> <HTML> <HEAD> WebForm1 Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load If Not Page.IsPostBack Then Session(&quot;tc&quot;) = 0 Session(&quot;bc&quot;) = 0 End If  End Sub

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Session(&quot;bc&quot;) = (Int32.Parse(Session(&quot;bc&quot;).ToString) + 1).ToString Response.Write(&quot; Button click times : &quot; + Session(&quot;bc&quot;)) End Sub

Private Sub TextBox1_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Session(&quot;tc&quot;) = (Int32.Parse(Session(&quot;tc&quot;).ToString) + 1).ToString Response.Write(&quot; Text change times : &quot; + Session(&quot;tc&quot;)) End Sub </HEAD> <body MS_POSITIONING=&quot;GridLayout&quot;> <form id=&quot;myform&quot; method=&quot;post&quot; runat=&quot;server&quot;> </asp:TextBox> <asp:Button id=&quot;Button1&quot; OnClick=Button1_Click style=&quot;Z-INDEX: 102; LEFT: 298px; POSITION: absolute; TOP: 207px&quot; runat=&quot;server&quot; Text=&quot;Button&quot;></asp:Button> </HTML>

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

<div class="references_section">