I need to do a postback and save the data in the screen to session when the screen is closed, for this i am using the onBeforeUnload event, and placed a hidden button in the screen. In the on before unload event I am calling the click event to call the button server event. But the event is not firing. Is anything I am missing here.
<asp:Button Style="display: none" runat="server" ID="btnHidUpdate" OnClick="btnHidUpdate_Click" />
<script type="text/javascript">
$(document).ready(function () {
window.onbeforeunload = updateSessionBeforeUnload;
});
</script>
In .js file:
function updateSessionBeforeUnload() {
var hidUpdate = $('[id$="btnHidUpdate"]')[0];
hidUpdate.click();
}
In .cs code behind:
protected void btnHidUpdate_Click(object sender, EventArgs e)
{
UpdateSession();
}