warn before leaving the page using asp.net? [duplicate]
What is the best way to detect if a user leaves a web page without first saving?
I have searched before I posted this question, but I haven't found related topics to deal with ASP.NET
(I have controls DropDownList
, ListBox
, TextBox
....)
I would use the JQuery serialize
method to save off the form's state in the page's OnLoad
event. Then serialize the page again in the OnBeforeUnload
event. If the values are different, then the page has changed. You'll have to add a flag to know whether or not the page is unloading because the "Save" button is being clicked. If the values are different and the user didn't click "Save", then display the "Do you want to save before leaving?" box.
One very simple way is to do it the StackOverflow way:
http://www.jonstjohn.com/node/23
Basically it boils down to setting a method to be called in the onbeforeunload javascript event.
精彩评论