Microsoft KB Archive/933791

= A new window loses focus when you set the DefaultFocus property or the DefaultButton property on a .NET Framework 2.0 ASP.NET Web site =

Article ID: 933791

Article Last Modified on 3/27/2007

-

APPLIES TO


 * Microsoft ASP.NET 2.0

-



INTRODUCTION
You set the DefaultFocus property or the DefaultButton property on a Microsoft .NET Framework 2.0 ASP.NET Web site. When you try to open a new window from a code behind event, the new window loses focus. The page where you set the DefaultFocus property or the DefaultButton property receives focus because you set the DefaultFocus property or the DefaultButton property.



MORE INFORMATION
By design, when you use the DefaultFocus property or the DefaultButton property, Microsoft ASP.NET creates client script that sets focus on a form control where the property is set when the page is rendered. When the new window receives focus, the windows that contain the form control also naturally receive focus. This causes the new window to lose focus.

Steps to reproduce the behavior
 In Microsoft Visual Studio 2005, click File, click New, and then click Web Site. Click ASP.NET Web Site, click HTTP in the Location list, type http://localhost/WebSite in the Location box, and then click OK. In Solution Explorer, right-click http://localhost/WebSite, and then click Add New Item. Click Web Form, type Popup.aspx in the Name box, and then click Add. In Solution Explorer, right-click Default.aspx, and then click View Designer. Add a TextBox control. Add a Button control.</li> In the Design window, double-click the Default.aspx page to create a page load event.</li>  Add the following code to the newly created Page_Load method. this.Page.Form.DefaultFocus = TextBox1.ClientID; </li> In Solution Explorer, right-click Default.aspx, and then click View Designer.</li> Double-click the Button control that you created in step 7 to create a button click event.</li>  In the Button1_Click method, add the following code. ClientScript.RegisterStartupScript(this.GetType, &quot;myWindow&quot;, String.Format(&quot; window.open('{0}'); &quot;, &quot;Popup.aspx&quot;)); </li> Click the Debug menu, and then click Start Debugging.</li> Click the Add a new Web.config file with debugging enabled option, and then click OK.</li> Click the Button control that you created in step 7 to open the new window.</li></ol>

Notice that the Default.aspx window receives the focus.

<div class="references_section">

STATUS
This behavior is by design.

Additional query words: DefaultFocus DefaultButton

Keywords: kbtshoot kbmsccsearch kbpubtypekc kbinfo KB933791

-

[mailto:TECHNET@MICROSOFT.COM Send feedback to Microsoft]

© Microsoft Corporation. All rights reserved.