简体   繁体   中英

open a window (aspx page) with one click + javascript

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);
} 

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)

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;";

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM