Silverlight - Prevent users from losing changes by using the browser back button


Do you know how to prevent users from losing changes in your Silverlight application, if they click the "back button" or "close browser" button?

Add this method to your main page (if using Silverlight navigation framework) or user control (if Silverlight app)

        public void RegisterOnBeforeUnload()
            //Register Silverlight object for availability in Javascript.
            const string scriptableObjectName = "SilverlightClientControl"; // <-- THIS IS THE NAME OF YOUR HOSTING OBJECT IN THE WEB PAGE
            HtmlPage.RegisterScriptableObject(scriptableObjectName, this);

            //Start listening to Javascript event.
            string pluginName = HtmlPage.Plugin.Id;
                @"window.onbeforeunload = function () {{
                   var slApp = document.getElementById('{0}');
                   var result = slApp.Content.{1}.OnBeforeUnload();
                   if(result.length > 0)
                       return result;
               }}", pluginName, scriptableObjectName)

Figure: Method that will register your Silverlight object for availability in JavaScript and start listening on JavaScript events


Call the top method "RegisterOnBeforeUnload" in the constructor of your control


Add this to the same user control

        public string OnBeforeUnload() 
            if (MainViewModel.HasUnsavedChanges) 
                return "You have unsaved changes! Please save them before leaving the application."; 
            return string.Empty; 

Figure: "MainViewModel.HasUnsavedChanges" is the flag if the exit is allowed without warning or not (= user clicks on "Exit Auction" which exits normally)



Figure: Now when you want the users is in the middle of a change and navigates away, he gets a nice warning!
This fires on the "Back button", "Close tab" or "Close browser", which is awesome!




You could even automagically save the changes when the user closes the browser!!


Unknown said...

Shouldn't this be..
if (MainViewModel.HasUnsavedChanges == true)

Peter Gfader said...

Well spotted Juri

This happens when you have to fix production code for blog posts...

louis vuitton zipped purse said...

Nice ! but how to the code for the page ~!

Anonymous said...

This is a great post - very helpful - Thanks

Anonymous said...

Perfect! Thank you :)

Ashok said...

Hi Peter Gfader,
I got below problem when am using ur above code.

Got Problem in the line of code:
HtmlPage.RegisterScriptableObject(scriptableObjectName, this);

Erroe is:
Object does not have a ScriptableAttribute or any scriptable members.

Please solve my prolem ASAP. Thanks in advance.


Peter Gfader said...

Check that the Silverlight object has an ID in the hosting page.
Check that you are using the right ID.

See this hint in code above:

Mbcoop said...

Many thanks Peter!

Ajay singh said...

Hi Peter.
may you explain MainViewModel.HasUnsavedChanges
i am not getting this line where you have declare this hasUnsavedChanges?
Please reply as soon as possible.

Peter Gfader said...


HasUnsavedChanges is a property on your ViewModel that tells you if the ViewModel is in "dirty" state.

you can just "return true" for testing purposes....

Ajay singh said...

Hi again.
i am new in silverlight and i am fresher in development field. my boss told me that you have to do any how @boss"i don't know how you will do".
so my question is when we will set the property true to the HasUnsavedChanges or we have to set that property manually to true when user is going to type any thing on the silverlight page.
please help me peter sir. otherwise i don't know what will happen with me.
May you send me the example if it is possible.
Ajay Singh

Peter Gfader said...

If you are using a "fancy" framework like Prism you might be able to just use "IsDirty" on the Object that you are binding to.

Otherwise manual. You might be able to solve this elegantly by hooking into INotifyPropertyChanged

Post a Comment

Latest Posts

Popular Posts