ASP.NET Page Close Confirmation
Posted on Friday, October 23, 2015
|
No Comments
triggering closing X browser event
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
<script type="text/javascript">
function OnBeforeUnLoadPage() {
if (document.getElementById('hdnRefresh').value == "") {
var result = confirm('Are you sure you want to navigate?');
if (result) {
UnLoadPage();
}
else {
return false;
}
}
}
function DetectRefreshKey() {
if (event.keyCode == 116) {
document.getElementById('hdnRefresh').value = "1";
}
}
function UnLoadPage() {
PageMethods.ClearSession();
}
function DetachBodyUnload() {
document.body.onbeforeunload = null;
document.body.onunload = null;
}
</script>
</head>
<body onbeforeunload="return OnBeforeUnLoadPage(); " onkeypress="DetectRefreshKey();">
<form id="form1" runat="server">
<input type="hidden" id="hdnRefresh" />
<asp:ScriptManager ID="sc" runat="server" EnablePageMethods="true" EnablePartialRendering="true">
</asp:ScriptManager>
<asp:Button ID="btnTest" runat="server" Text="Refresh" OnClientClick="DetachBodyUnload();"
OnClick="btnTest_Click" />
</form>
</body>
</html>
code behind:
protected void Page_Load(object sender, EventArgs e)
{
HttpContext.Current.Session["test"] = "TEST DATA";
}
[WebMethod]
public static void ClearSession()
{
HttpContext.Current.Session.Remove("test");
}
protected void btnTest_Click(object sender, EventArgs e)
{
}
http://forums.asp.net/t/1580541.aspx