Solved

Close window on EventHandler??

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

What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

A quick way to get a menu to work on our website, is using the Menu control and assign it to a web.sitemap using SiteMapDataSource. Example of web.sitemap file: (CODE) Sample code to add to the page menu: (CODE) Running the application, we wi…
Introduction This article shows how to use the open source plupload control to upload multiple images. The images are resized on the client side before uploading and the upload is done in chunks. Background I had to provide a way for user…
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.
This video explains how to create simple products associated to Magento configurable product and offers fast way of their generation with Store Manager for Magento tool.

744 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now