MrDave's (David Yack) WebLog

Warning leaving this page will lose data...

Ok, well leaving this page won't lose data but woudn't it be nice if there was a page level property NavigationWarnUser=true that would warn the user if they changed any input field on the form and are about to navigate away from the page?  Who has the asp.net 3.0 wish list?

Has anyone come up with a nice reusable framework for handling this? 

In theory you could package up some generic rountines that would grab all the values form input fields on load of the page, then upon unload compare and prompt the user.  The big challenge I see is the fact that control over the unload process is more of a notification than a change to do anything.

Anyone have some great ideas on this one?

 

Comments

Dennis v/d Stelt said:

A developer at a project I was on once build this functionality. Dunnow how he did it, but if I'm not mistaken, he did every field on the screen again in a hidden field with a slightly different name. Then in the onUnload event (or whetever it's called) he executed some javascript that checked if values were changed and displayed a confirm (javascript)box.
# December 16, 2003 2:25 AM

Johnny Hall said:

I did it a slightly clunky way, not perfect but...

Add a hidden field with a boolean value (via a specific navigation server control). Then add a piece of javascript to each control which sets the boolean value to true (on the onchange event, or onclick, etc.).

On postback (via navigation) check the value and then pass this to this page in an event argument.

It was a specific solution for a specific problem but it worked.
# December 16, 2003 4:27 AM

AndrewSeven said:

Take a look at the built-in javascript for validation, it has routines for dealing with groups of controls.

You could try implementing a custom validator with client script that always returns valid but which sets a dirty flag in a var or hidden.


You could also look at the BizDesk from Commerce Server, it has "unsaved changes" warnings.

I think the biggest issue will be implementing it cross-browser.
# December 16, 2003 9:18 AM

Jon said:

test
# April 7, 2004 11:40 AM

elizas said:

Hi, this is a good one.I am also a similar tips to share with you.

Page life cycle includes two events like onunload and onbeforeunload. For this case you need to bind the script function in window. Onbeforeunload so that it will be called when page is unloading.Again this warning should not be fired when you are actually submitting the page. For that set a boolean value (e.g. shouldsubmit) to submit the page.

http://bit.ly/92RTZZ

# January 27, 2010 5:18 AM

Alice Powell said:

I think the solution to this is too simple to have occurred to you. Just add a second switch variable in javascript (e.g. isPostback), that you set to true on the javascript event that triggers the postback. You can add these event handlers in code-behind. For instance, for your dropdownlist you would

DropDownList1.Attributes.Add("onchange", "isPostback = true;");

ASP.NET appends the dopostback to the end of your functions. In your onbeforeunload function you'd just also check the value of the switch. I have used this approach in similar situations.

# May 4, 2011 5:30 AM
Leave a Comment

(required) 

(required) 

(optional)

(required)