We help IT Professionals succeed at work.

auto save form information before changing another page or closing the browser

Kyle Foster
Kyle Foster asked
on
1,289 Views
Last Modified: 2008-07-21
I have an AJAX ASP.NET 3.5 (2.0) form that works terrific (hurrah for AJAX)
What I want to do is have the form automatically save anytime the browser is redirected to another page and/or the browser is closed.  (ie.  user back button, user clicks a link on the page...)

I expect that there is a certain Javascript event that I need to catch such as unload, but not being well versed in web programming I am not sure the best way to handle this.

It would be nice if I could popup a confirm dialog box and then if the user clicks on save, I will call a server side event to save the data after which the redirect continues as if nothing happened.
Comment
Watch Question

Jens FiedererTest Developer/Validator

Commented:
Internet Explorer does, indeed, have an onbeforeunload event (on the document object) you can hook to detect page transitions or exit.

Unfortunately, these events work differently among browsers.

There is a discussion at
http://www.webdeveloper.com/forum/archive/index.php/t-98785.html

CERTIFIED EXPERT

Author

Commented:
I have javascript code for onbeforeunload
---------
window.onbeforeunload = bunload;

function bunload(){
  mess = "You will lose all information provided\nduring navigation of this site";
  return mess;
}

but I am not sure how to call the c# SaveData() method from Javascript

Jens FiedererTest Developer/Validator

Commented:
Ah .... you can't call Server methods from the client directly, you would have to expose your "SaveData()" as a webmethod and use AJAX to call it (and ignore the result).
CERTIFIED EXPERT

Author

Commented:
jensfiederer,

This sounds like what I want to do.  The question is how do I "expose the SaveData() as a webmethod and call it from AJAX" ?
CERTIFIED EXPERT

Author

Commented:
I am reposting the last comment to push out an email to jensfiederer
-----------
This sounds like what I want to do.  The question is how do I "expose the SaveData() as a webmethod and call it from AJAX" ?

Any ideas?
Test Developer/Validator
Commented:
This one is on us!
(Get your first solution completely free - no credit card required)
UNLOCK SOLUTION
Unlock the solution to this question.
Join our community and discover your potential

Experts Exchange is the only place where you can interact directly with leading experts in the technology field. Become a member today and access the collective knowledge of thousands of technology experts.

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.