After reading this post you may want to kick me for my stupidity. But this issue was raised by one of our client, so I have to look into this problem. So bear with me and please read the entire question.
The home page of my application is not meant to be unloaded automatically. Only when the user clicks the logout button or refreshes the page, I am supposed to do the logout operation.
There are few update panels on the page. One update panel contains a hidden asp.net button which is used to trigger an AsyncPostback by a JavaScript function.
If I mouse click on any control anywhere on the page and then do not release the mouse button and the JavaScript function is triggered, which tries to click on the asp.net button to trigger a server side operation. The AsyncPostback caused by the update panel turns into a FullPostback.
I think after the request ends the updatepanel is unable to update the UI thus triggers a full postback.
I wrote a sample application to simulate this problem. Check out the source code:
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="AjaxBodyUnload._Default" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>Untitled Page</title>
<script type="text/javascript">
var timer;
var btnId = '<%= Button1.ClientID %>';
function BodyUnload() {
alert("Unloaded");
}
function DoAsyncPostBack() {
var btn = document.getElementById(btnId);
btn.click();
}
window.onload = function() {
timer = setInterval(DoAsyncPostBack, 3000);
}
</script>
</head>
<body onunload="BodyUnload()">
<form id="form1" runat="server">
<asp:ScriptManager ID="ScriptManager1" runat="server" />
<a href="javascript:void(0)" title="Click & Hold Me">Click & Hold Me</a>
<asp:UpdatePanel ID="UpdatePanel1" runat="server" UpdateMode="Conditional">
<ContentTemplate>
<asp:Label ID="Label1" runat="server" Text="Label"></asp:Label>
<asp:Button ID="Button1" runat="server" Text="Button" OnClick="Button1_Click" style="display:none" />
</ContentTemplate>
</asp:UpdatePanel>
</form>
</body>
</html>
Just click and hold on the link that says "Click & Hold Me" for few seconds and you will see that the page gets unloaded.
Is there any any event/callback I could handle in ASP.NET Ajax Extensions to cancel the post back in such scenario?