Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations bkrike on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Closing a form 1

Status
Not open for further replies.

croydon

Programmer
Apr 30, 2002
253
EU
Is there a simple way to close a form?

I have an application that will send an email to a user asking them to enter feedback into a specified URL (.NET page). After they have opened the form and entered the data, they would click on a button which updates the database. After this I would like the form to automatically close (rather than re-directing somewhere). Is there a way of doing this? Thanks.
 
Using javascript you can close the window like this..
Code:
window.close();
You will need to emit the javascript in a script block after saving to the database. I suggest using the Page method RegisterClientScriptBlock like this
Code:
string script = "<script type=\"text/javascript\">window.close();</script>";
Page.RegisterClientScriptBlock("close", script);

Rob

Go placidly amidst the noise and haste, and remember what peace there may be in silence - Erhmann 1927
 
Rob,
Can you please tell me when "window.close" would run? Before you posted your reply, I was thinking of telling croydon to add a "Close" button to the form, and call window.close() from there. Of course, I didn't do it because that would have been a very cheap solution. I didn't know how to add script that would run right after a postback.

In any case, my question is: Do the scripts added with RegisterClientScriptBlock run automatically without being called. If yes, when do they run?

Thanks!

JC

Friends are angels who lift us to our feet when our wings have trouble remembering how to fly...
 
Thanks for your help. I'll give it a try.
 
After your code executes you can do something like this....


'code to do button actions
Code:
Me.Page.RegisterClientScriptBlock("Close","<script language=JavaScript>window.close()</script>")


oops...same as above....should work though...
 
JC

The browser runs any javascript which is not within a function as the page loads. So in the above example, the script block will be rendered to the response stream and as the browser "gets" to the command it will close the window. Any scrips inside a function will only execute when the function is called. An example below....
Code:
<script type="text/javascript">
// This code runs as the page loads
var i = 1;
alert(i);
i++;
alert(i);
function increment(){
  // This code only runs when the function is called
  i++;
  alert(i);
}
// This code runs as the page loads
increment();
<\script>
<input type="button" name="button" value="increment" onclick="increment();" />
Ok so as the browser loads in the above page it will alert '1', increment i and then alert '2', then call the increment function and alert '3'. After the page has loaded every time the button is clicked i will be incremented and alerted so the first time the browser would alert '4'.

HTH

Rob

Go placidly amidst the noise and haste, and remember what peace there may be in silence - Erhmann 1927
 
Thanks Rob
This is good to know as I could now write javascript that would run after server code executes. I didn't know how to do this before. Thanks again. Here's your well-earned star!

JC

Friends are angels who lift us to our feet when our wings have trouble remembering how to fly...
 
Thanks for your help with this, just one last thing:

Is there any way to stop the following message appearing:

"The web page you are viewing is trying to close the window. Do you want to close the window?"
<yes> <no>"



 
Yes, if the page you're closing was called by another one of your pages you should be able to do this...

I'll try to find my code for this...

dlc
 
checkai, I have done some searching and I expect you mean: window.opener = self;
Thanks.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top