Notice: This website is an unofficial Microsoft Knowledge Base (hereinafter KB) archive and is intended to provide a reliable access to deleted content from Microsoft KB. All KB articles are owned by Microsoft Corporation. Read full disclaimer for more details.

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


View products that this article applies to.

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.

↑ Back to the top


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

  1. In Microsoft Visual Studio 2005, click File, click New, and then click Web Site.
  2. Click ASP.NET Web Site, click HTTP in the Location list, type http://localhost/WebSite in the Location box, and then click OK.
  3. In Solution Explorer, right-click http://localhost/WebSite, and then click Add New Item.
  4. Click Web Form, type Popup.aspx in the Name box, and then click Add.
  5. In Solution Explorer, right-click Default.aspx, and then click View Designer.
  6. Add a TextBox control.
  7. Add a Button control.
  8. In the Design window, double-click the Default.aspx page to create a page load event.
  9. Add the following code to the newly created Page_Load method.
    this.Page.Form.DefaultFocus = TextBox1.ClientID;
  10. In Solution Explorer, right-click Default.aspx, and then click View Designer.
  11. Double-click the Button control that you created in step 7 to create a button click event.
  12. In the Button1_Click method, add the following code.
    ClientScript.RegisterStartupScript(this.GetType(), "myWindow", String.Format("<script>window.open('{0}');</script>", "Popup.aspx"));
  13. Click the Debug menu, and then click Start Debugging.
  14. Click the Add a new Web.config file with debugging enabled option, and then click OK.
  15. Click the Button control that you created in step 7 to open the new window.
Notice that the Default.aspx window receives the focus.

↑ Back to the top


Status

This behavior is by design.

↑ Back to the top


Keywords: KB933791, kbinfo, kbtshoot

↑ Back to the top

Article Info
Article ID : 933791
Revision : 4
Created on : 3/27/2007
Published on : 3/27/2007
Exists online : False
Views : 336