I'm trying to catch when a user leaves a page (content page inside a master page) so I can update a flag in a database (the database isn't visible to the client PC so I can't code a link to it client side).
Currently I can catch the page unload with jQuery firing the __doPostBack() event, which I then catch server side.
However, this then stops the redirect from firing unless I code it as part of the code behind for the postback. As there are two buttons that are triggering this postback event that require different behaviour, this is causing a problem.
I've tried setting a viewstate to determine if the pageunload is caused by one of the two buttons, and stepping through the code I can see the button code fire correctly and updates the viewstate to the correct value, but when this then triggers the PageUnload code, the viewstate reverts to the previous value.
I've tried a hidden field, but the page isn't rebuilt before unloading the page so the hidden field still holds the previous value.
I've tried a page variable but again the value doesn't hold.
Is there a way to tell the code behind to continue with the redirect, or to set a flag that will help me trigger the correct redirect?
Current code: (on content page)
$(document).ready(function(){
$(window).unload(function(){
__doPostBack('Unload','');
});
});
(on page.load, code behind)
Dim eventtarget as String
Me.ClientScript.GetPostBackEventReference(Me, String.Empty)
If (Me.Request("__EVENTTARGET") is nothing) then
eventtarget = string.empty
else
eventtarget = Me.Request("__EVENTTARGET")
End If
If eventtarget = "Unload" then
closepage()
End If
closepage() just fires off the code I need to execute (record the page has been closed in the database).
Thanks.