What is the best way to close an ASPX page from the code-behind?
I have a button event handler that I want to close the page after the user has clicked an ASP.NET button on the page. I have tried to programmatically add a JavaScript method that contains a window.close()
command to the OnClientClick
event to close the page but it does not work. The button is also a asp:AsyncPostBoskTrigger
for an AJAX Update Panel.
The application uses .NET Framework 3.5.
Best Answer
You would typically do something like:
However, keep in mind that different things will happen in different scenerios. Firefox won't let you close a window that wasn't opened by you (opened with
window.open()
).IE7 will prompt the user with a "This page is trying to close (Yes | No)" dialog.
In any case, you should be prepared to deal with the window not always closing!
One fix for the 2 above issues is to use:
And create a close.html:
Note that close.html will redirect to default.aspx if the window does not close after 2 sec for some reason.