0

I'm working under ASP.NET and I wonder if is possible to display a custom save prompt (let's say a div with styles, a message and some buttons) when the user is trying to leave the page.
I've seen the onunload and onbeforeunload JS events.
The onunload event seems to does not work for my purpose, because I display the message but I can't prevent the user from leaving the page.
The onbeforunload could be useful just because I can prevent the user from leaving the page, but the prompt is the responsibility of the navigator, and I want to implement it!

I think there is not another option than onbeforeunload to warn the user about possible losses of work but I expect helpful answers.

Related question: Prompting user to save when they leave a page

Alex
  • 1,457
  • 1
  • 13
  • 26
mati
  • 5,218
  • 3
  • 32
  • 49
  • Dupe: http://stackoverflow.com/questions/1209231/is-there-anyway-to-know-that-user-leaving-a-page-with-asp-net – David Basarab Aug 25 '09 at 16:26
  • Is not a dupe, It's just about the same theme. However, I'm closing this since I discovered there is no alternative for onbeforeunload navigator popup. Thanks anyway. – mati Aug 25 '09 at 18:02

3 Answers3

1

None of those events will work. Remember ASP.NET is stateless. Those are events that are run when the page is sent from IIS to the client, meaning they have nothing to do if the user leaves they are events that clean up ASP.NET.

See this answer on this question.

Community
  • 1
  • 1
David Basarab
  • 72,212
  • 42
  • 129
  • 156
1

There is not another solution than using onbeforeunload JavaScript event. The prompt is implemented by each navigator independently, and just a piece of text can be displayed.

window.onbeforeunload = function(){ 
    return 'I want to show this text. Do u want to leave?'; 
};
Alex
  • 1,457
  • 1
  • 13
  • 26
mati
  • 5,218
  • 3
  • 32
  • 49
0

The very page you are currently visiting works with ASP.NET, and if you start typing an answer and navigate away, a message box pops up and allows you to stay on the page. Don't ask me how it's done, it's probably visible in the page sources. I'm not sure whether this behavior is customizable, but I'd use it as a starting point.

Malte Clasen
  • 5,637
  • 1
  • 23
  • 28