Skip to main content
Email Password   helpLost your password?

Introduction

This article describes on how you can implement the functionality of saving changes on close of the browser or navigating away from the current web application. It also discusses on various ways that the user can exit the page how data be saved on those events.

Background

Many have been trying to implement the functionality of Saving the data on close of the browser. I was also one of you trying to find a way on how to implement the same. I had been to many forums and discussion boards, but couldn't find a way on how to implement this. I finally landed up at a particular piece of javascript code (attached below) to trap the close of IE window which actually helped me in completing this functionality.

Using the Code

Navigation in a site can happen in two ways. Following are the list of actions by which users can navigate.

Following are the ways through which the user can navigate away from the page. All these are covered in this solution.

Navigation or postback using controls available in the application.

Navigation through user actions, browser buttons, shortcut keys, close of the browser. Following are a list of ways by which this is achieved.

Identifying the different actions and enabling save for each action is a tedious process. The save functionality can be implemented in a simpler way using the javascript event onbeforeunload.

Reason to use onbeforeunload:

The onbeforeunload event (which is called before unload event) is called when

Thus onbeforeunload is the appropriate event at which the save functionality can be implemented.

Implementation of the Save Functionality:

To differentiate between a user initiated event and a control initiated event an attribute is being added named tag with a value "DonotCallSaveonLoad" to all the known controls in the application using which the save method should be called without prompting the user to save the page.

Screenshot - Attributes-Add.png

The event.srcElement will set the object of the control to the variable currentElement which initiated the postback. This will be set on the onClick event of the control.

Note: onClick event is the only point where the Control object can be retrieved because the object of the control is destroyed when the process reaches the onbeforeUnload event.

The onbeforeUnload event calls the HandleOnClose function which checks if the variable currentElement has a value or not.

Screenshot - body-load.png

The javascript function HandleOnClose checks for this attribute tag of the currentElement object. If the currentElement is null or the value of the attribute tag of the currentElement is not DonotCallSaveonLoad, then it calls the function FunctiontoCallSaveData, otherwise the save functionality on the code behind is called.

Screenshot - Handle-Close.png

The implementation of the function FunctiontoCallSaveData is the same as previously sent which is as follows.

Screenshot - Save-Data_Function.png

The form.submit will in turn call Page_Load in which the Save functionality can be called. Error Handling can also be performed for this functionality. The same is shown in the code snippet below.

Screenshot - Page-Load.png

Points to Note

This functionality cannot be debugged using Breakpoints. This functionality can be verified by the checking if the data has been updated in the datasource. In the attached demo, there is a SQL script which creates a temporary table to which data is stored on close of the browser. Please make necessary changes to the function SaveDataonPage on the .vb file as per your convinience. Exception Handling is implemented using Enterprise Library 2.0. Customizations can be done to it Exception Handling also.
You must Sign In to use this message board.
 
 
Per page   
 FirstPrevNext
GeneralThanks Pin
Falguni Barik
4:30 11 Sep '09  
GeneralASP.net masterpage Pin
naveenIIT
18:39 12 Jan '09  
GeneralRe: ASP.net masterpage Pin
fuzzballb
1:24 30 Oct '09  
GeneralServer Postback Issue... Pin
Rohan Christian
13:08 2 Oct '07  
GeneralRe: Server Postback Issue... Pin
Abishek R Srikaanth
22:31 3 Oct '07  
GeneralRe: Server Postback Issue... Pin
Rohan Christian
9:45 4 Oct '07  
Generalmissed some events Pin
skullkiller
11:15 25 Sep '07  
AnswerRe: missed some events Pin
Abishek R Srikaanth
22:27 3 Oct '07  
GeneralIncomplete (defective) code Pin
fwsouthern
0:10 13 Sep '07  
GeneralRe: Incomplete (defective) code Pin
Abishek R Srikaanth
21:18 24 Sep '07  
GeneralVery helpful Pin
samerh
3:04 11 Sep '07  
GeneralRe: Very helpful Pin
ricobano
6:10 23 Oct '07  
GeneralNice Pin
Pooya Musavi
11:05 7 Sep '07  
AnswerRe: Nice Pin
Rodrigo Rodriguez
5:48 12 Sep '07  
Questiondoesn't work with FireFox Pin
barbod_blue
21:50 1 Sep '07  
AnswerRe: doesn't work with FireFox Pin
Abishek R Srikaanth
21:51 3 Sep '07  
Questiondoesn't work with FireFox Pin
barbod_blue
21:49 1 Sep '07  
GeneralGood Article Pin
merlin981
5:53 29 Aug '07  
GeneralRe: Good Article Pin
Thanks for all the fish
10:07 29 Aug '07  
GeneralRe: Good Article Pin
merlin981
12:47 29 Aug '07  
GeneralRe: Good Article Pin
Vuyiswamb
21:03 31 Oct '07  
Generaldetect changes made? and go to other page? Pin
Thanks for all the fish
4:32 29 Aug '07  


Last Updated 24 Sep 2007 | Advertise | Privacy | Terms of Use | Copyright © CodeProject, 1999-2009