Solved

Close window on EventHandler??

Posted on 2004-08-28
2
355 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

Active Directory Webinar

We all know we need to protect and secure our privileges, but where to start? Join Experts Exchange and ManageEngine on Tuesday, April 11, 2017 10:00 AM PDT to learn how to track and secure privileged users in Active Directory.

Question has a verified solution.

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

Suggested Solutions

One of the pain points with developing AJAX, JavaScript, JQuery, and other client-side behaviors is that JavaScript doesn’t allow for cross domain request for pulling content. For example, JavaScript code on www.johnchapman.name could not pull conte…
Today is the age of broadband.  More and more people are going this route determined to experience the web and it’s multitude of services as quickly and painlessly as possible. Coupled with the move to broadband, people are experiencing the web via …
Microsoft Active Directory, the widely used IT infrastructure, is known for its high risk of credential theft. The best way to test your Active Directory’s vulnerabilities to pass-the-ticket, pass-the-hash, privilege escalation, and malware attacks …
Email security requires an ever evolving service that stays up to date with counter-evolving threats. The Email Laundry perform Research and Development to ensure their email security service evolves faster than cyber criminals. We apply our Threat…

830 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