Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Programmatically close aspx page from code behind

Tags:

c#

asp.net

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.

like image 974
Michael Kniskern Avatar asked Dec 17 '08 17:12

Michael Kniskern


People also ask

How to close aspx page from code behind c#?

Response. Write("<script>window. close();</script>");

How can I see the code behind ASPX?

Right-click the . aspx page, and then click View Code. The code-behind file opens in the editor.

How do you exit ASP?

You can use Response. Close(). This will stop execution of next statements.


2 Answers

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 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:

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.

like image 146
CodingWithSpike Avatar answered Sep 24 '22 17:09

CodingWithSpike


 protected void btnOK_Click(object sender, EventArgs e)         {            // Your code goes here.           if(isSuccess)           {                   string  close =    @"<script type='text/javascript'>                                 window.returnValue = true;                                 window.close();                                 </script>";             base.Response.Write(close);             }          } 
like image 42
SP007 Avatar answered Sep 21 '22 17:09

SP007