Recently, I found an interesting problem when using server controls (e.g. asp:button) to launch javascript popup windows. For example, the following code should be able to pop up a window (the javascript for popUp() is omitted here):

<asp:Button ID=”Button1″ runat=”server” CausesValidation=”False” OnClientClick=”javascript:popUp()”

Text=”Test” />

And it works as long as you don’t have a default button or default focus on the form. If you add either a default button or a default focus or both to the form, the popup window will become a pop under window (i.e. it stays underneath the window from which the popup is launched).

<form id=”form1″ runat=”server” defaultbutton=”Button1″ defaultfocus=”TextBox1″>

So, how to solve this problem? Well, basically there are two ways. The first one is to use an HTML input element instead of the asp:Button control:

<input id=”Button1″ type=”button” value=”Test” onclick=”javascript: popUp()”/>

And the second approach is (as we did before ASP.Net 2.0) to incorporate some client side javascript to set default button and default focus instead of relying on the defaultbutton and defaultfocus attributes in the form tag.

Be Sociable, Share!