5

I am saving records on save button click, if user don't click save button and navigate to another page while clicking on some link even then i want to call the save method. How can I achieve this functionality?

please provide some sample code ...

thanks in advance for your help

Joel Coehoorn
  • 399,467
  • 113
  • 570
  • 794
Kash
  • 245
  • 1
  • 4
  • 14
  • Have you tried anything? – Simon Oct 12 '11 at 13:53
  • Follow this: http://stackoverflow.com/questions/1821625/ajax-request-with-jquery-on-page-unload – Riz Oct 12 '11 at 13:53
  • 2
    You should really not ask for the code, but for the rectification. You should try before you ask. – Amin Sayed Oct 12 '11 at 13:55
  • Remember: there is no way to guarantee this code will execute. For example, if the browser executable itself is forcibly stopped (or crashes), or the computer's connection to the internet is broken, or its power cord yanked, etc... In other words, use this technique as a convenience, but NEVER for system-critical code. – mikemanne Oct 12 '11 at 15:12

3 Answers3

6

You can make ajax request on

window.onbeforeunload = function(){ 
    ////make ajax request
}

Or can prevent the user by giving confirm box

function showalert() {
  if (!confirm('Are you sure you want to exit without saving changes?')) { 
     ////make ajax request
  }
  else {return true;}
}

window.onbeforeunload = function(){ showalert }

For example check this out when I leave this page while answering SO prevent me

enter image description here

Wazy
  • 8,822
  • 10
  • 53
  • 98
  • i have added following code, 'function AjaxRequestOnLeave(arguments) { var ajaxManager = $find("<%= RadAjaxManager1.ClientID %>"); if (ajaxManager != null) ajaxManager.ajaxRequest(''); } window.onunload = AjaxRequestOnLeave();' but it runs after page load, but it dont fires on page leave. – Kash Oct 13 '11 at 07:48
  • it is JavaScript function, I am using a third party control for ajax call, `window.onunload = AjaxRequestOnLeave();` but it fires only after page load. – Kash Oct 13 '11 at 07:57
  • Actually It's a browser-specific behaviour. Chrome and FF will send a GET requst BEFORE onunload is fired, IE8 will execute onunload first. Not sure, how the other browser handle it. – Wazy Oct 13 '11 at 08:07
1

Use an ajax post, triggered by window.OnBeforeUnload() to let yourself know that the user has left the page, and pass any information you need.

echo_Me
  • 37,078
  • 5
  • 58
  • 78
1000003
  • 11
  • 1
  • i have added following code, `function AjaxRequestOnLeave(arguments) { var ajaxManager = $find("<%= RadAjaxManager1.ClientID %>"); if (ajaxManager != null) ajaxManager.ajaxRequest(''); } window.onunload = AjaxRequestOnLeave();` **but it runs after page load, but it dont fires on page leave.** – Kash Oct 13 '11 at 08:02
1

Well since you want to call the save method even if the user navigates to another page or link what u need to do is set a hidden field for eg. suppose hdnSave and set its value to say 0 Now when the user navigates or clicks on any another link first check if this hidden field value(hdnSave) is set to 1 or not.If not then Call Save Method.So this hidden Field can be used as an indicator of whether Save Method has been called or not.

Similarly you can use a session(in c# code) for the same purpose as well.

Jeff Walker Code Ranger
  • 4,634
  • 1
  • 43
  • 62
iJade
  • 23,144
  • 56
  • 154
  • 243