Solved

Close window on EventHandler??

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

This article discusses the ASP.NET AJAX ModalPopupExtender control. In this article we will show how to use the ModalPopupExtender control, how to display/show/call the ASP.NET AJAX ModalPopupExtender control from javascript, how to show/display/cal…
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…
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.

932 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