简体   繁体   English

一键+ javascript打开一个窗口(aspx页)

[英]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. 我需要单击一个asp.net按钮打开一个新窗口。 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 : JavaScript函数:

 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. 问题是您要添加代码来处理click事件处理程序中的单击。 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: 在Page_Load中添加属性,以便按钮始终具有该属性,或者在事件处理程序中,将代码添加到回发后立即调用该函数的页面:

ClientScript.RegisterStartupScript(Me.GetType(), "open", "OpenPopup();", True)

This problem likely occurs because the form in being submitted on click (PostBack). 发生此问题的原因可能是单击(PostBack)时提交了表单。 If you return false in the onclick attribute, it should cancel a form submit. 如果在onclick属性中返回false,则应取消表单提交。 You can also use OnClientClick . 您也可以使用OnClientClick

btnClaim.OnClientClick = "javascript:OpenPopup(); return false;";

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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