views:

57

answers:

2

I have been trying to close the window on button click but am unable to do so.

I have added javascript window.close()

added on the code behind page on button click event all in vain. Language c# or vb.net

+1  A: 

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:Button ID="close" runat="server" OnClientClick="javascript:window.close()" />

Although that would be a bit pointless. =)

Coding Gorilla
+1  A: 

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.

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.

ShaunO