Microsoft KB Archive/810203

= PRB: Control Focus Is Lost After AutoPostBack =

Article ID: 810203

Article Last Modified on 5/18/2007

-

APPLIES TO


 * Microsoft ASP.NET 1.1
 * Microsoft Visual Basic .NET 2003 Standard Edition
 * Microsoft Visual C# .NET 2003 Standard Edition
 * Microsoft ASP.NET 1.0
 * Microsoft Visual Basic .NET 2002 Standard Edition
 * Microsoft Visual C# .NET 2002 Standard Edition

-



SYMPTOMS
When you press the TAB key on an ASP.NET Web Server control, the Web page is posted back to the Web server. After the operation is complete, the focus is not set to any control. You expect the focus to be set to the next control.



CAUSE
This problem occurs because the AutoPostBack property of the control is set to True.



STATUS
This behavior is by design.



MORE INFORMATION
If the AutoPostBack property is set to True, postback occurs when you press ENTER or when the control loses the focus after the contents are modified.

To work around this behavior:   In the HTML view of WebForm1.aspx, replace the existing code with the following:

Visual C# .NET Code <%@ Page Language=&quot;C#&quot; AutoEventWireup=&quot;false&quot; Codebehind=&quot;WebForm1.aspx.cs&quot; Inherits=&quot;WebApplication11.WebForm1&quot;%> <!DOCTYPE HTML PUBLIC &quot;-//W3C//DTD HTML 4.0 Transitional//EN&quot;>   Label  Label <asp:Label id=&quot;Label3&quot; runat=&quot;server&quot; style=&quot;LEFT: 12px; POSITION: absolute; TOP: 95px&quot;>Label</asp:Label></asp:textbox> <asp:Button id=&quot;Button1&quot; runat=&quot;server&quot; Text=&quot;Button&quot; style=&quot;LEFT: 85px; POSITION: absolute; TOP: 136px&quot;></asp:Button> <script language=&quot;javascript&quot;> document.all[&quot;<%= Focus %>&quot;].focus; if(&quot;<%= ControlType %>&quot; == &quot;TextArea&quot;) document.all[&quot;<%= Focus %>&quot;].select; </HTML> Visual Basic .NET Code <%@ Page Language=&quot;vb&quot; AutoEventWireup=&quot;false&quot; Codebehind=&quot;WebForm1.aspx.vb&quot; Inherits=&quot;WebApplication11.WebForm1&quot;%> <!DOCTYPE HTML PUBLIC &quot;-//W3C//DTD HTML 4.0 Transitional//EN&quot;> <HTML> <form id=&quot;Form1&quot; method=&quot;post&quot; runat=&quot;server&quot;> <asp:Label id=&quot;Label1&quot; runat=&quot;server&quot; style=&quot;LEFT: 10px; POSITION: absolute; TOP: 20px&quot;>Label</asp:Label> </asp:textbox> <asp:Label id=&quot;Label2&quot; runat=&quot;server&quot; style=&quot;LEFT: 11px; POSITION: absolute; TOP: 54px&quot;>Label</asp:Label></asp:textbox> <asp:Label id=&quot;Label3&quot; runat=&quot;server&quot; style=&quot;LEFT: 12px; POSITION: absolute; TOP: 95px&quot;>Label</asp:Label></asp:textbox> <asp:Button id=&quot;Button1&quot; runat=&quot;server&quot; Text=&quot;Button&quot; style=&quot;LEFT: 85px; POSITION: absolute; TOP: 136px&quot;></asp:Button> <script language=&quot;javascript&quot;> document.all[&quot;<%= Focus %>&quot;].focus; if(&quot;<%= ControlType %>&quot; == &quot;TextArea&quot;) document.all[&quot;<%= Focus %>&quot;].select; </HTML> </li> <li>In Solution Explorer, right-click WebForm1.aspx, and then click View Code.</li> <li> In the code-behind class, replace the existing code with the following:

Visual C# .NET Code using System; using System.Collections; using System.ComponentModel; using System.Data; using System.Drawing; using System.Web; using System.Web.SessionState; using System.Web.UI; using System.Web.UI.WebControls; using System.Web.UI.HtmlControls;

namespace WebApplication22 {   public class WebForm1 : System.Web.UI.Page {       protected System.Web.UI.WebControls.Label Label1; protected System.Web.UI.WebControls.TextBox TextBox1; protected System.Web.UI.WebControls.Label Label2; protected System.Web.UI.WebControls.TextBox TextBox2; protected System.Web.UI.WebControls.Label Label3; protected System.Web.UI.WebControls.TextBox TextBox3; protected System.Web.UI.WebControls.Button Button1; protected string Focus; protected string ControlType;

private void Page_Load(object sender, System.EventArgs e)       { Focus = &quot;TextBox1&quot;; ControlType = &quot;TextArea&quot;; }

#region Web Form Designer generated code override protected void OnInit(EventArgs e)       { InitializeComponent; base.OnInit(e); }       private void InitializeComponent {               this.TextBox1.TextChanged += new System.EventHandler(this.TextBox1_TextChanged); this.TextBox2.TextChanged += new System.EventHandler(this.TextBox2_TextChanged); this.TextBox3.TextChanged += new System.EventHandler(this.TextBox3_TextChanged); this.Load += new System.EventHandler(this.Page_Load); }       #endregion

private void TextBox1_TextChanged(object sender, System.EventArgs e)       { Focus = &quot;TextBox2&quot;; ControlType = &quot;TextArea&quot;; }       private void TextBox2_TextChanged(object sender, System.EventArgs e)        { Focus = &quot;TextBox3&quot;; ControlType = &quot;TextArea&quot;; }       private void TextBox3_TextChanged(object sender, System.EventArgs e)        { Focus = &quot;Button1&quot;; ControlType = &quot;Button&quot;; }   } }  Visual Basic .NET Code Public Class WebForm1 Inherits System.Web.UI.Page Protected WithEvents Label1 As System.Web.UI.WebControls.Label Protected WithEvents TextBox1 As System.Web.UI.WebControls.TextBox Protected WithEvents Label2 As System.Web.UI.WebControls.Label Protected WithEvents TextBox2 As System.Web.UI.WebControls.TextBox Protected WithEvents Label3 As System.Web.UI.WebControls.Label Protected WithEvents TextBox3 As System.Web.UI.WebControls.TextBox Protected WithEvents Button1 As System.Web.UI.WebControls.Button Protected Focus As String Protected ControlType As String


 * 1) Region &quot; Web Form Designer Generated Code &quot;

'This call is required by the Web Form Designer. <System.Diagnostics.DebuggerStepThrough> Private Sub InitializeComponent

End Sub

Private Sub Page_Init(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Init 'CODEGEN: This method call is required by the Web Form Designer 'Do not modify it using the code editor. InitializeComponent End Sub


 * 1) End Region

Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load Focus = &quot;TextBox1&quot; ControlType = &quot;TextArea&quot; End Sub Private Sub TextBox1_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextBox1.TextChanged Focus = &quot;TextBox2&quot; ControlType = &quot;TextArea&quot; End Sub

Private Sub TextBox2_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextBox2.TextChanged Focus = &quot;TextBox3&quot; ControlType = &quot;TextArea&quot; End Sub

Private Sub TextBox3_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextBox3.TextChanged Focus = &quot;Button1&quot; ControlType = &quot;Button&quot; End Sub End Class </li> <li>On the Build menu, click Start to view WebForm1.aspx in the browser.</li> <li>Type text in the text box, and then press the TAB key. The focus is set to the next control after the post back operation.</li></ol>

Steps to Reproduce the Behavior
<ol> <li>Create a new ASP.NET Web Application project. By default, WebForm1.aspx is created.</li> <li> In the HTML view of WebForm1.aspx, replace the existing code with the following code:

Visual C# .NET Code <%@ Page Language=&quot;C#&quot; AutoEventWireup=&quot;false&quot; Codebehind=&quot;WebForm1.aspx.cs&quot; Inherits=&quot;WebApplication11.WebForm1&quot;%> <!DOCTYPE HTML PUBLIC &quot;-//W3C//DTD HTML 4.0 Transitional//EN&quot;> <HTML> <form id=&quot;Form1&quot; method=&quot;post&quot; runat=&quot;server&quot;> <asp:Label id=&quot;Label1&quot; runat=&quot;server&quot; style=&quot;LEFT: 10px; POSITION: absolute; TOP: 20px&quot;>Label</asp:Label> </asp:textbox> <asp:Label id=&quot;Label2&quot; runat=&quot;server&quot; style=&quot;LEFT: 11px; POSITION: absolute; TOP: 54px&quot;>Label</asp:Label></asp:textbox> <asp:Label id=&quot;Label3&quot; runat=&quot;server&quot; style=&quot;LEFT: 12px; POSITION: absolute; TOP: 95px&quot;>Label</asp:Label></asp:textbox> <asp:Button id=&quot;Button1&quot; runat=&quot;server&quot; Text=&quot;Button&quot; style=&quot;LEFT: 85px; POSITION: absolute; TOP: 136px&quot;></asp:Button> </HTML> Visual Basic .NET Code <%@ Page Language=&quot;vb&quot; AutoEventWireup=&quot;false&quot; Codebehind=&quot;WebForm1.aspx.vb&quot; Inherits=&quot;WebApplication11.WebForm1&quot;%> <!DOCTYPE HTML PUBLIC &quot;-//W3C//DTD HTML 4.0 Transitional//EN&quot;> <HTML> <form id=&quot;Form1&quot; method=&quot;post&quot; runat=&quot;server&quot;> <asp:Label id=&quot;Label1&quot; runat=&quot;server&quot; style=&quot;LEFT: 10px; POSITION: absolute; TOP: 20px&quot;>Label</asp:Label> </asp:textbox> <asp:Label id=&quot;Label2&quot; runat=&quot;server&quot; style=&quot;LEFT: 11px; POSITION: absolute; TOP: 54px&quot;>Label</asp:Label></asp:textbox> <asp:Label id=&quot;Label3&quot; runat=&quot;server&quot; style=&quot;LEFT: 12px; POSITION: absolute; TOP: 95px&quot;>Label</asp:Label></asp:textbox> <asp:Button id=&quot;Button1&quot; runat=&quot;server&quot; Text=&quot;Button&quot; style=&quot;LEFT: 85px; POSITION: absolute; TOP: 136px&quot;></asp:Button> </HTML> </li> <li>On the Debug menu, click Start to run the project.</li></ol>

<div class="references_section">