views:

33

answers:

2

Hi all, I need to open a new window with a single click of an asp.net button. My problem is that it always takes two clicks.. if i write the open window code in the page load, then the window gets open on 1 click.. any ideas how to get around this...

Button Click Code :

btnClaim.Attributes.Add("Onclick","javascript:return OpenPopup()")

Javascript function :

  function OpenPopup()
{
window.open("newWindow.aspx?", "_blank", "height=500, width=575, left=150,
top=150, " +
"location=no, menubar=no, resizable=no, " +
"scrollbars=no, titlebar=no, toolbar=no", true);
}  
+2  A: 

This problem likely occurs because the form in being submitted on click (PostBack). If you return false in the onclick attribute, it should cancel a form submit. You can also use OnClientClick.

btnClaim.OnClientClick = "javascript:OpenPopup(); return false;";
Jordan
No, that doesn't cause the need for clicking twice. It may cause another postback at the second click though, after the window has been opened.
Guffa
+2  A: 

The problem is that you are adding code to handle a click in the click event handler. That means that when you click the button the first time the event handler adds the attribute to the button. After the postback you can click the button again to open the popup, as the button now has the client side code.

Either add the attribute in Page_Load so that the button always has the attribute, or in the event handler add code to the page that calls the function immediately after postback:

ClientScript.RegisterStartupScript(Me.GetType(), "open", "OpenPopup();", True)
Guffa
Thank you Guffa.
BumbleBee