tags:

views:

7233

answers:

6

What is the best way to close aspx page from the code behind?

I have an button eventhandler 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.

It is an ASP.NET C# 3.5 application.

+4  A: 

A postback is the process of re-loading a page, so if you want the page to close after the postback then you need to set your window.close() javascript to run with the browser's onload event during that postback, normally done using the ClientScript.RegisterStartupScript() function.

But are you sure this is what you want to do? Closing pages tends to piss off users.

Joel Coehoorn
A: 

You should inject a startup script that will close the page after the postback has finished.

ClientScript.RegisterStartupScript(typeof(Page), "closePage", "<script type='text/JavaScript'>window.close();</script>");
EndangeredMassa
It used that line of code to my page, and it closed it right away. I need to close th page once the user clicks the ASP.NET button control on the page.
Michael Kniskern
Put it in the button onclick event handler in the codebehind.
EndangeredMassa
+1  A: 

UPDATE: I have taken all of your input and came up with the following solution:

In code behind:

protected void Page_Load(object sender, EventArgs e)    
{
    Page.ClientScript.RegisterOnSubmitStatement(typeof(Page), "closePage", "window.onunload = CloseWindow();");
}

In aspx page:

function CloseWindow() {
    window.close();
}
Michael Kniskern
+4  A: 

You would typically do something like:

protected void btnClose_Click(object sender, EventArgs e)
{
    ClientScript.RegisterStartupScript(typeof(Page), "closePage", "window.close();", true);
}

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 a window.open() call).

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:

protected void btnClose_Click(object sender, EventArgs e) {
    ClientScript.RegisterStartupScript(typeof(Page), "closePage", "window.open('close.html', '_self', null);", true);
}

And create a close.html:

<html><head>
 <title></title>
 <script language="javascript" type="text/javascript">
     var redirectTimerId = 0;
     function closeWindow()
     {
         window.opener = top;
         redirectTimerId = window.setTimeout('redirect()', 2000);
         window.close(); 
     }

     function stopRedirect()
     {
         window.clearTimeout(redirectTimerId);
     }

     function redirect()
     {
         window.location = 'default.aspx';
     }
 </script>
 </head>
 <body onload="closeWindow()" onunload="stopRedirect()" style="">
     <center><h1>Please Wait...</h1></center>
 </body></html>

Note that close.html will redirect to default.aspx if the window does not close after 2 sec for some reason.

rally25rs
+1  A: 

You can close a window by simply pasting the window closing code in the button's OnClientClick event in the markup

Perpetualcoder
A: 

Hi Michael Kniskem! I tried you code,I had the same problem It does work with IE6 but when tried it in firefox .it does not close the window and after 2 sec it redirects to default.aspx.Is there any fix to this issue.

You did try the solution below? I have only tested in IE 7
Michael Kniskern