There is a non-standard onbeforeunload function you can use to detect if the user is leaving the page. It works in almost all the browsers except Opera (it follows W3C standards strictly). The problem with this function is that it will also annoy users when any external link is clicked or the form is submitted. You may use jQuery and do some code-tweaking to avoid it. Try following example:
<head runat="server">
<title></title>
<script src="Scripts/jquery-1.8.3.min.js" type="text/javascript"></script>
<script language="javascript" type="text/javascript">
window.onbeforeunload = function () {
return "Are you sure you want to leave this page?";
};
$(document).ready(function () {
$('a[rel!=ext]').click(function () { window.onbeforeunload = null; });
$('form').submit(function () { window.onbeforeunload = null; });
});
</script>
</head>
<body>
<form id="form1" runat="server">
<div>
<a href="http://www.google.com" rel="ext">Redirect with message</a>
<br />
<a href="http://www.google.com">Redirect without message</a><br />
<asp:Button ID="btnSave" runat="server" Text="Save" />
</div>
</form>
</body>
</html>