|
||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Announcements
Services
Chapters
Feature Zones
|
IntroductionThis code enables a Web page to notify users that they are leaving a page that they have made modifications to. Why is this useful? Well, the previous sentence says it all. We want to notify the user that they have made modifications to a page and ask them if they really want to leave the page. It is assumed that you are familiar with JavaScript, DOM Events, and ASP.NET 1.x or higher. The included project is in Visual Studio 2005. BackgroundThe idea of notifying users that they are leaving a page that has been modified is not new. There are plenty of sites that explain this process via client-scripting of the Using the CodeThe core of this article is quite simple. JavaScript does all the magic. All we need to do is tie this JavaScript magic into the ASP.NET client-side submission model. So, as you can see, in the JavaScript object, // Register the event.
window.onbeforeunload = function() {
return MySuperDuper.checkForChanges();
}
Here's the full client-side code: // This script works in Internet Explorer 6+, FireFox and Safari.
// I believe Opera might handle it too, but I did not test.
var MySuperDuper = new function() {
// Internal reference to the class
// to avoid the "this" pointer issue
// when dealing with events.
var var _this = this;
//you can declare this if you want, but I don't see the point.
//this.globalChange = undefined;
// Determines whether or not a form is being saved.
this.saving;
// Message to display.
this.navigateAwayMessage = "You have made some changes which" +
" will be lost if you leave this page. " +
"Do you want to leave this page?";
// Verify if any changes were made. If there
// have been changes and the form is not being saved,
// alert the user that they are about to leave
// a page where they have made edits.
this.checkForChanges = function() {
if (!_this.saving && typeof(_this.globalChange) !=
"undefined" && _this.globalChange)
{
if(document.all && event)
event.returnValue = _this.navigateAwayMessage;
else
return _this.navigateAwayMessage;
}
}
}
// Register the event.
window.onbeforeunload = function() {
return MySuperDuper.checkForChanges();
}
Here's the server-side code to incorporate the JavaScript magic. In my project, I put this in a ClientScript.RegisterOnSubmitStatement(typeof(BasePage),
"saving", "MySuperDuper.saving = true;");
Now for your control, simply update the myTextBox.attributes.Add("onchange", "MySuperDuper.globalChange = true;");
You could also get the <!--
We would just need to handle any event for changes in our controls.
This can be done inline like below or attached. I leave it up to you.
-->
<input onchange="MySuperDuper.globalChange = true;" type="text" />
Also, after getting some feedback from crashedapp (see below), he offered his suggestion to check if there have been changes. Thanks for the feedback. So say crashedapp had his own way of checking for changes, he could just override my method function yourCustomFunction()
{
// ... mind blowing code here
// return true or false
}
MySuperDuper.checkForChanges = yourCustomFunction
Points of InterestIt has been tested on Internet Explorer 6+, FireFox, and Safari for Windows. Internet Explorer and FireFox handle navigating away from a page as does Safari, but when you close the browser window and changes have been made, only FireFox and Internet Explorer fire the This will also work with AJAX. All that you need to manage is when a partial render has come back to the client, you just need to reset History
|
|||||||||||||||||||||||||||||||||||||||||||||||||||