Click here to Skip to main content
6,594,432 members and growing! (17,314 online)
Email Password   helpLost your password?
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 Srikaanth

This article will show how to save changes on close of the browser or when exiting a page.
Javascript, SQL, VB 8.0, Windows, .NET 2.0, ASP.NET, WebForms, VS.NET2003, VS2005, DBA, Dev
Posted:28 Aug 2007
Updated:24 Sep 2007
Views:71,030
Bookmarked:83 times
Unedited contribution
Announcements
Loading...
 
Search    
Advanced Search
Add to IE Search
printPrint   add Share
      Discuss Discuss   Broken Article?Report  
14 votes for this article.
Popularity: 4.63 Rating: 4.04 out of 5
1 vote, 7.1%
1
1 vote, 7.1%
2
3 votes, 21.4%
3
2 votes, 14.3%
4
7 votes, 50.0%
5

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.

  • Navigating to pages using controls provided within the site.
  • Navigating to pages using Browser Controls, shortcut keys or the right click options.

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.

  • Image Buttons
  • Link Buttons
  • Submit Buttons
  • Server Controls with "Autopostback" option set to True

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

  • Back button on the Browser Toolbar
  • Refresh button on the Browser Toolbar
  • Close button on the Browser Toolbar
  • Home button on the Browser Toolbar
  • ALT + F4 to close the browser
  • CTRL + F4 (for tabbed browsers)
  • F5 or CTRL + R or Right click for refresh
  • ALT + Home button (to navigate to client's Home page)
  • Click backspace (alternative for Back button)
  • Typing a URL and navigating to a different page
  • Selecting a URL from the favorites button
  • Right Click and select Refresh

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

  • Navigating to another page (as mentioned above)
  • Postback due to a control event
  • Using controls on the Browser
  • Closing the page.

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.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

About the Author

Abishek R Srikaanth


Member
Currently working as the Associate Technical Project Manager with ShipNet Software Solutions.

I have fun researching on the .NET Technologies and finding new ideas to create rich programs.
Occupation: Program Manager
Company: ShipNet Software Solutions India Pvt. Ltd.
Location: India India

Other popular ASP.NET articles:

Article Top
You must Sign In to use this message board.
FAQ FAQ 
 
Noise Tolerance  Layout  Per page   
 Msgs 1 to 22 of 22 (Total in Forum: 22) (Refresh)FirstPrevNext
GeneralThanks PinmemberFalguni Barik4:30 11 Sep '09  
GeneralASP.net masterpage PinmembernaveenIIT18:39 12 Jan '09  
GeneralRe: ASP.net masterpage Pinmemberfuzzballb1:24 30 Oct '09  
GeneralServer Postback Issue... PinmemberRohan Christian13:08 2 Oct '07  
GeneralRe: Server Postback Issue... PinmemberAbishek R Srikaanth22:31 3 Oct '07  
GeneralRe: Server Postback Issue... PinmemberRohan Christian9:45 4 Oct '07  
Generalmissed some events Pinmemberskullkiller11:15 25 Sep '07  
AnswerRe: missed some events PinmemberAbishek R Srikaanth22:27 3 Oct '07  
GeneralIncomplete (defective) code Pinmemberfwsouthern0:10 13 Sep '07  
GeneralRe: Incomplete (defective) code PinmemberAbishek R Srikaanth21:18 24 Sep '07  
GeneralVery helpful Pinmembersamerh3:04 11 Sep '07  
GeneralRe: Very helpful Pinmemberricobano6:10 23 Oct '07  
GeneralNice PinmemberPooya Musavi11:05 7 Sep '07  
AnswerRe: Nice PinmemberRodrigo Rodriguez5:48 12 Sep '07  
Questiondoesn't work with FireFox Pinmemberbarbod_blue21:50 1 Sep '07  
AnswerRe: doesn't work with FireFox PinmemberAbishek R Srikaanth21:51 3 Sep '07  
Questiondoesn't work with FireFox Pinmemberbarbod_blue21:49 1 Sep '07  
GeneralGood Article Pinmembermerlin9815:53 29 Aug '07  
GeneralRe: Good Article PinmemberThanks for all the fish10:07 29 Aug '07  
GeneralRe: Good Article Pinmembermerlin98112:47 29 Aug '07  
GeneralRe: Good Article PinmemberVuyiswamb21:03 31 Oct '07  
Generaldetect changes made? and go to other page? PinmemberThanks for all the fish4:32 29 Aug '07  

General General    News News    Question Question    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

PermaLink | Privacy | Terms of Use
Last Updated: 24 Sep 2007
Editor:
Copyright 2007 by Abishek R Srikaanth
Everything else Copyright © CodeProject, 1999-2009
Web10 | Advertise on the Code Project