Solved

Close window on EventHandler??

Posted on 2004-08-28
2
357 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

In an ASP.NET application, I faced some technical problems. In this article, I list them out and show the solutions that I found.  I hope it will be useful. Problem: After closing a pop-up window, the parent page should be refreshed automaticall…
Problem Hi all,    While many today have fast Internet connection, there are many still who do not, or are connecting through devices with a slower connect, so light web pages and fast load times are still popular.    If your ASP.NET page …

734 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