Microsoft Knowledge Base Email Alertz

KBAlertz.com: 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

Receive Microsoft Knowledge Base articles by E-Mail?

Every night we scan the Microsoft Knowledge Base. If technologies you're interested in are updated, we'll send you an e-mail. You only get one e-mail a day, and only when new articles are added.

Click here to create a
FREE account
Already have an account?
[Click here to Login]

Search KbAlertz

Advanced Search

Webmasters
Put kbAlertz on your website.
[ Click Here for more! ]






Microsoft Knowledge Base Article

This article contents is Microsoft Copyrighted material.
©2005-©2007 Microsoft Corporation. All rights reserved. Terms of Use | Trademarks

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

Retired KB ArticleThis article was written about products for which Microsoft no longer offers support. Therefore, this article is offered "as is" and will no longer be updated.
Article ID:933791
Last Review:March 27, 2007
Revision:1.1
On This Page

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.

Back to the top

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

REFERENCES

For more information, visit the following Microsoft Developer Network (MSDN) Web sites:
http://msdn2.microsoft.com/en-us/library/system.web.ui.htmlcontrols.htmlform.defaultbutton.aspx (http://msdn2.microsoft.com/en-us/library/system.web.ui.htmlcontrols.htmlform.defaultbutton.aspx)
http://msdn2.microsoft.com/en-us/library/system.web.ui.htmlcontrols.htmlform.defaultfocus.aspx (http://msdn2.microsoft.com/en-us/library/system.web.ui.htmlcontrols.htmlform.defaultfocus.aspx)

Back to the top

STATUS

This behavior is by design.

Back to the top


APPLIES TO
•Microsoft ASP.NET 2.0

Back to the top

Keywords: 
kbtshoot kbmsccsearch kbpubtypekc kbinfo KB933791

Back to the top

   

Community Feedback System

Very often, it takes hours to solve a problem. Very often, you've looked high and low, and have tried a lot of solutions. When you finally found it, chances are, it was because someone else helped you. Here's your chance to give back. Use our community feedback tool to let others know what worked for you and what didn't.

Please also understand that the community feedback system is not warranted to be correct, it's simply a system that we've built to let people try and help each other. If something in a feedback response doesn't make sense to you, or you're not comfortable making changes that the feedback talks about (like registry edits), please consult a professional.

Thank you for using kbAlertz.com Feedback System.

-- Scott Cate

Be the first to leave feedback, to help others about this knowledge base article.

(Optional) Name

(Optional) Public URL Or Email

Comments
No HTML -- Text Only Please