what client-side and server-side events are fired when button X is hit?

Folks,
In a web aspx page, I use a client-side (javascript) button named "Cancel" so when users hit it, if the data they enter is not saved yet, then this client-side button prompts users to save data before the page is closed. I code this client-side button with event "onclick" and thing like
   My question is if users hit the right top corner button X (next to the maximize and minimize buttons also on the top right corner of the aspx web window page), then what client-side (javascript) and server-side (C#) events are fired when button X is hit?

 Thanks in advance.

-1
johnaspnet
4/16/2006 2:48:33 PM
📁 asp.net.client-side
📃 24353 articles.
⭐ 2 followers.

💬 10 Replies
👁️‍🗨️ 2100 Views

Hey John!

The event thats fired is called the "onbeforeunload" event in the HTML page, this code should fire when you close your browser.

"... 

<script language="JavaScript">
window.onload = function()
{
	window.onbeforeunload = function()
	{
		alert("sure you want to close?");
	}
}
script>

 ..."

However, this code also runs when you refresh the browser, so you have to let JavaScript check the X,Y coordinates to determine if the event was called from anywhere near the 'X' button.

Good luck!

0
Eray
4/16/2006 6:20:51 PM
Hi Eray,
 Thanks very much for your reply for client-side. I do not know what event is fired with server-side (C# event handler). If you know, please post it in this topic.
 
0
johnaspnet
4/17/2006 1:49:12 PM

There is no >>event is fired with server-side (C# event handler)<< since it is a client-side event of the DOM/browser.

NC...

0
NC01
4/17/2006 3:31:26 PM
If it is a client side event, then you can please tell me what is it? I tried to use JS event "onunload" but the event handler is always called when the page is firstly loaded and when the page is finishes its loading. This behavior is acceptable when users hit button X of the browser but it is not acceptable when the page finishes its first loading (not post back) on the client side.
0
johnaspnet
12/27/2006 10:21:25 PM

server side : in the global.asax page, the Session_End event will fire when the user session is cleared or removed or when the user closed the web application.

http://builder.com.com/5100-6371-5771721.html

client side : you can handle the onbeforeunload of the body tag event using javascript.

Happy Coding


Haissam Abdul Malak
MCAD.NET
| Blog |
0
Haissam
12/27/2006 11:26:54 PM

johnaspnet:

If it is a client side event, then you can please tell me what is it?

Client-side, the onbeforeunload event is fired AND then the onunload event is fired.

johnaspnet:

I tried to use JS event "onunload" but the event handler is always called when the page is firstly loaded and when the page is finishes its loading. This behavior is acceptable when users hit button X of the browser but it is not acceptable when the page finishes its first loading (not post back) on the client side.

Of course those events will ALWAYS be fired if the page is closed. It is up to you to differentiate when it is a PostBack or not.

An example:

Add to the CodeBehind, PageLoad event handler:
      Page.RegisterOnSubmitStatement("OnSubmitScript", "g_isAPostBack = true;");

NC...

0
NC01
12/28/2006 11:56:23 AM
If users carelessly hit broswer's X button, how do they can have the last chance (like an client-side confirm message dialog open saying "Do you want to save data?") to keep the aspx page open so they can save the input data if they select button "OK" on that js confirm button? If they select "Cancel" from the confirm dialog, then the page will close and the data is not saved.
0
johnaspnet
12/28/2006 10:52:34 PM

Change the OnBeforeUnload event handler to the following and when the Cancel button is pressed, the page will stay opened. That is the only option that you have, as it is a web page.


0
NC01
12/30/2006 11:26:45 AM

NC01 is not online. Last active: 12-30-2006, 7:32 AM NC01 ,
Thanks so much. My page now is working in the way I would like. However, this seems to work with IE but not FireFox or NetScape?

0
johnaspnet
12/30/2006 11:36:05 PM

I believe that  window.event.returnValue is an IE only property. Sorry.

NC...

 

0
NC01
12/31/2006 11:12:05 AM