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
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
Share Improve this question edited Nov 16, 2011 at 16:39 Joel Coehoorn 416k114 gold badges578 silver badges813 bronze badges asked Oct 12, 2011 at 13:49 KashKash 2451 gold badge4 silver badges15 bronze badges 4- Have you tried anything? – Simon Commented Oct 12, 2011 at 13:53
- Follow this: stackoverflow./questions/1821625/… – Riz Commented Oct 12, 2011 at 13:53
- 2 You should really not ask for the code, but for the rectification. You should try before you ask. – Amin Sayed Commented Oct 12, 2011 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 puter'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 Commented Oct 12, 2011 at 15:12
3 Answers
Reset to default 6You 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
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.
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.