![]() |
Web Development »
ASP.NET »
General
Intermediate
License: The Code Project Open License (CPOL)
Save Changes on Close of Browser or when exiting the page.By Abishek R SrikaanthThis article will show how to save changes on close of the browser or when exiting a page. |
Javascript, SQL, VB8.0, Windows, .NET2.0, ASP.NET, WebForms, VS.NET2003, VS2005, DBA, Dev
|
||||||||||
|
Advanced Search Add to IE Search |
|
|
|
||||||||||||||||
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.
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.
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.
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.
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.

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.

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.

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

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.
General
News
Question
Answer
Joke
Rant
Admin
Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads.
|
PermaLink |
Privacy |
Terms of Use
Last Updated: 24 Sep 2007 Editor: |
Copyright 2007 by Abishek R Srikaanth Everything else Copyright © CodeProject, 1999-2010 Web10 | Advertise on the Code Project |