You will have to use JavaScript to do this.
The
OnBeforeUnload Event is a neat feature used by web developers to let the user cancel the page submit if they need to.
What you'll have to do is add a HiddenField to the page indicating whether or not the page is dirty.
Write a JavaScript function that will handle the OnBeforeUnload JavaScript event. This function will check the HiddenField, and if the page is dirty will return "You have unsaved changes. Are you sure you want to leave the page?".
The only thing is that this event will fire every time the page is unloaded.
So you'll also need to write a bit of JavaScript that determines whether or not to display the message depending on if the page is posting back for reasons other than leaving current page.
Note: that the onbeforeunload event happens when the whole page is being unloaded....this means that if you're using Ajax this event will not fire.
If you just want to check if the page is dirty when the user clicks on some control that leads the user away from your page then you could just call the function that checks if the page is dirty during the OnClick (or onChange) event (client side) for the control.
If you want to inform the user that they will lose information if they are leaving the page by hitting the back button, or typing a new url ...etc...then use the OnBeforeUnload event.
-Frinny