I have the following code to detect user navigation or close.
The post back is triggered on close but not when navigated away.
If i place a alert message it is triggered at all times.
Oh and there is a timer on the page which is actually disabled after first tick. but there is always a post back triggers when navigating away with eventtarget timer. I think this may be because i have cleared cache for the previous page.
<script src="Scripts/jquery-1.4.4.min.js" type="text/javascript"></script>
<script type="text/javascript">
$(window).bind("beforeunload", function () {
alert("u r navigating away"); // this message gets triggered at all times.
__doPostBack('callPostBack');
});
</script>
protected void Page_Load(object sender, EventArgs e)
{
if (IsPostBack)
{
string eventTarget = this.Request["__EVENTTARGET"];
string eventArgument = this.Request["__EVENTARGUMENT"];
if (eventTarget != String.Empty && eventTarget == "callPostBack")
{
//do task
}
}
}
Thanks in advance.
This is not logical, when the user go way from the page or close it, you can not actually make a post back, think about, the user close the page and you try to reload it with a post back? And if your action works then you have a dead loop, you make post back and keep open the page that user try to close. Anyway if the user close the page, or change it, this is first priority and the post back are not working.
If you try to make ajax call there you also fails because the ajax stops after the page close and this is going to be done (the page close) before the ajax trigger.
You need to re-desing your action.