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.
- In the Design window, double-click the Default.aspx page to create a page load event.
- Add the following code to the newly created Page_Load method.
this.Page.Form.DefaultFocus = TextBox1.ClientID;
- In Solution Explorer, right-click Default.aspx, and then click View Designer.
- Double-click the Button control that you created in step 7 to create a button click event.
- In the Button1_Click method, add the following code.
ClientScript.RegisterStartupScript(this.GetType(), "myWindow", String.Format("<script>window.open('{0}');</script>", "Popup.aspx"));
- Click the Debug menu, and then click Start Debugging.
- Click the Add a new Web.config file with debugging enabled option, and then click OK.
- Click the Button control that you created in step 7 to open the new window.
Notice that the Default.aspx window receives the focus.