Solved

Close window on EventHandler??

Posted on 2004-08-28
2
350 Views
Last Modified: 2011-09-20

Hello, new .Net (using C#). I have a button event handler that I want to close the window (it's popup) when it's done doing some work and return to another page. How can I accomplish this??

I know how to close a window and return to the parent I came from in javascript, can I somehow in javascript call the event handler in the javascript before closing the window?

time sensitive issue for me, thanks in advance!!

0
Comment
Question by:animated405
2 Comments
 
LVL 8

Accepted Solution

by:
shovavnik earned 500 total points
ID: 11924714
Not directly, no.

But you can post back when the user clicks on a button and in the event handler for the button add a RegisterStartupScript.

Button_Click() {
  string scriptString = "<script>window.close();</script>";
  RegisterStartupScript( "startup", scriptString );
}

This will cause asp.net to emit the script before the closing tag of the <form> element, which will close the window as soon the browser parses that script.

Basically, the solution to your problem is to post back to the server and have the server either send a script to the client, or alternatively, leave a message on the client that the window needs to be closed.  If you prefer the second solution, it should look like this:

<head>
<script>
function pageLoad() {
  var checkboxCloseWindow = document.all[ 'CheckBoxCloseWindow' ];
  if( checkboxCloseWindow.checked ) window.close();
}
</script>
</head>
<body onLoad="pageLoad()">
<asp:CheckBox id="CheckBoxCloseWindow" runat="server" style="display:none;" Checked="False"></asp:CheckBox>
<asp:Button id="MyButton" runat="server" OnClick="MyButton_Click">Click Me</asp:Button>
</body>

and then on the server:
... MyButton_Click() {
  // if we want to close the window when the browser parses the response stream:
  CheckBoxCloseWindow.Checked = true;
}
0
 

Author Comment

by:animated405
ID: 11926154

Excellent very helpfull!! I went with the options to RegisterStartScript and it works like a charm...
0

Featured Post

Optimizing Cloud Backup for Low Bandwidth

With cloud storage prices going down a growing number of SMBs start to use it for backup storage. Unfortunately, business data volume rarely fits the average Internet speed. This article provides an overview of main Internet speed challenges and reveals backup best practices.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

In this Article, I will provide a few tips in problem and solution manner. Opening an ASPX page in Visual studio 2003 is very slow. To make it fast, please do follow below steps:   Open the Solution/Project. Right click the ASPX file to b…
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
This Micro Tutorial hows how you can integrate  Mac OSX to a Windows Active Directory Domain. Apple has made it easy to allow users to bind their macs to a windows domain with relative ease. The following video show how to bind OSX Mavericks to …
This video shows how to quickly and easily add an email signature for all users on Exchange 2016. The resulting signature is applied on a server level by Exchange Online. The email signature template has been downloaded from: www.mail-signatures…

810 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question