简体   繁体   English

关闭按钮单击窗口

[英]Close Window for button click

I have been trying to close the window on button click but am unable to do so. 我一直试图在按钮点击时关闭窗口,但我无法这样做。

I have added javascript window.close() 我添加了javascript window.close()

added on the code behind page on button click event all in vain. 在按钮点击事件的页面后面的代码上添加都是徒劳的。 Language c# or vb.net 语言c#vb.net

That needs to be on the client side, try something like this: 这需要在客户端,尝试这样的事情:

<input type="button" id="close" onclick="window.close()" />

If you want an asp.net button you can do: 如果你想要一个asp.net按钮,你可以这样做:

<asp:Button ID="close" runat="server" OnClientClick="javascript:window.close()" />

Although that would be a bit pointless. 虽然那会有点无意义。 =) =)

The button click event on the code behind only handles code that affects the server side. 后面代码上的按钮单击事件仅处理影响服务器端的代码。 Closing a browser window is a client side action and must be called by something on the browser. 关闭浏览器窗口是客户端操作,必须通过浏览器上的某些内容进行调用。 This is usually done with an input button but can be used inside any kind of javascript event. 这通常使用输入按钮完成,但可以在任何类型的javascript事件中使用。

Here is an example that I pulled out of existing code, the extra calls were used for browser compatibility. 这是我从现有代码中提取的一个示例,额外的调用用于浏览器兼容性。

<input type="button" onclick="window.opener=null; window.close(); return false;" />

Also of note, browsers may block this action if it is not initiated by a user action. 另外值得注意的是,如果浏览器不是由用户操作启动,则浏览器可能会阻止此操作。

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

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