Wrox Programmer Forums
Go Back   Wrox Programmer Forums > ASP.NET and ASP > ASP.NET 2.0 > ASP.NET 2.0 Basics
|
ASP.NET 2.0 Basics If you are new to ASP or ASP.NET programming with version 2.0, this is the forum to begin asking questions. Please also see the Visual Web Developer 2005 forum.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the ASP.NET 2.0 Basics section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old November 4th, 2005, 01:50 PM
Authorized User
 
Join Date: Jul 2004
Posts: 25
Thanks: 0
Thanked 0 Times in 0 Posts
Default Need exit routine in ASPX page

I am looking for an routine in an ASPX web page that is called when the user leaves the page or the page closes or the page "unloads". This would be the opposite of Page_Load or Page_PreRender where these are called when the page first loads up. I am using C# .Net ASP.

Thank you,

Paul

 
Old November 4th, 2005, 05:59 PM
Imar's Avatar
Wrox Author
 
Join Date: Jun 2003
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts
Default

I think this is more related to client side scripting than to .NET or .NET 2.0.

A web page runs on the server, so when the user closes here browser no event runs ont he server.

Take a look at these these posts for some ideas and opinions on this:

You *could* try this with the onunload of a page with client side JavaScript, but it'll be messy.
http://p2p.wrox.com/topic.asp?TOPIC_...,browser,close
http://p2p.wrox.com/topic.asp?TOPIC_...,browser,close
http://p2p.wrox.com/topic.asp?TOPIC_...,browser,close

Cheers,

Imar
---------------------------------------
Imar Spaanjaars
Everyone is unique, except for me.
While typing this post, I was listening to: Erics Trip by Sonic Youth (Track 5 from the album: Daydream Nation) What's This?
 
Old November 4th, 2005, 06:01 PM
Authorized User
 
Join Date: Jul 2005
Posts: 59
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Page_Unload() ?

Some suggest using this for disconnecting database connection.

 
Old November 4th, 2005, 06:18 PM
Authorized User
 
Join Date: Jul 2004
Posts: 25
Thanks: 0
Thanked 0 Times in 0 Posts
Default

The java script will not work for me. I need to call a routine in my C# code behind, which makes a call to a database to save data on the web page. I will check out Page_Unload().

Thank you,

Paul

 
Old November 4th, 2005, 06:21 PM
Imar's Avatar
Wrox Author
 
Join Date: Jun 2003
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts
Default

Page_Onload fires when the page is finished rendering; that is, it fires when the page is complete and sent to the browser, thus *before* the user has even started reading it. It fires for each request and won't be of much use to your requirements.

Please read the three links I posted to see how and why this is a problem.....

Imar
---------------------------------------
Imar Spaanjaars
Everyone is unique, except for me.
While typing this post, I was listening to: Lily (My One and Only) by The Smashing Pumpkins (Track 12 from the album: Mellon Collie and the Infinite Sadness - Twilight to Starlight) What's This?
 
Old November 4th, 2005, 08:53 PM
Authorized User
 
Join Date: Jul 2004
Posts: 25
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Thanks for the links. Very interesting people on these formums.

Paul

 
Old November 4th, 2005, 09:05 PM
Authorized User
 
Join Date: Jul 2004
Posts: 25
Thanks: 0
Thanked 0 Times in 0 Posts
Default

I am thinking of adding creating a onbeforeunload() that has this:

function ConfirmClose()
{
 var x = confirm('Notes need to be saved. Do you want to continue?');
 if(x == true) {
 alert('Yes, continue.');
 } else {
 alert('No, keep current page up.');
 }
}

If the user clicks NO and wants not to exit or go to the next page, how do I keep the current page up?

 
Old November 4th, 2005, 09:12 PM
Imar's Avatar
Wrox Author
 
Join Date: Jun 2003
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts
Default

You're welcome.

You can return false from the function:

else {
 alert('No, keep current page up.');
  return false;
 }

You can shorten it down to:
Code:
function ConfirmClose()
{
  return confirm('Notes need to be saved. Do you want to continue?');
}
Cheers,

Imar
---------------------------------------
Imar Spaanjaars
Everyone is unique, except for me.
While typing this post, I was listening to: Fire In The Head by The Tea Party (Track 1 from the album: The Edges Of Twilight) What's This?
 
Old November 4th, 2005, 09:17 PM
Imar's Avatar
Wrox Author
 
Join Date: Jun 2003
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts
Default

Ooops, answered to quick.

That would work for normal JavaScript methods, but not for this one.

Consider this:

<body onbeforeunload="return false;">

That would stop the browser from closing for ever if it was possible. Good for advertisement agencies that want you to view a lot of their banners.... ;)

Cheers,

Imar
---------------------------------------
Imar Spaanjaars
Everyone is unique, except for me.
While typing this post, I was listening to: Deus by The Sugarcubes (Track 8 from the album: Life's Too Good) What's This?
 
Old November 8th, 2005, 02:37 PM
Authorized User
 
Join Date: Jul 2004
Posts: 25
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Imar,

Thanks for the code. The:

<body onbeforeunload="return false;">

prompts the user if he or she would like to continue on to the next page or stay at the current page, but I need to display a better message box that says something about specific to my page.

How come the following does not work?

function ConfirmClose()
{
  return confirm('Notes need to be saved. Do you want to continue?');
}








Similar Threads
Thread Thread Starter Forum Replies Last Post
sub Routine problem NitinJoshi General .NET 1 January 5th, 2005 04:10 AM
How to pass variables from Aspx page to Asp Page jayaraj Classic ASP Basics 2 May 23rd, 2004 06:56 AM
How to pass the variables in Aspx page to Asp Page jayaraj ASP.NET 1.0 and 1.1 Basics 3 May 23rd, 2004 06:55 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.