To begin with I'm new to Web forms and JavaScript and I've spent two days Googling with no results. I'm trying to ask the user, before he exits the page whether they want to save the data they've input. I've got a textbox that tells whether any input has been performed which should trigger the javascript if statement. My code is working if the user moves away from the page using a control contained within the page. It is not working when they click the X in the upper right-hand corner or select File/Exit. Below is my code.
<script type="text/javascript" >
var needToConfirm = "False";
var hdnNeedToConfirm = ""
window.onbeforeunload = confirmExit;
function confirmExit() {
__doPostBack('<%= Page.Master.FindControl("MainContent").FindControl("PADForm").ClientID %>', '');
__doPostBack('<%= Page.Master.FindControl("MainContent").FindControl("HiddenPanel").ClientID %>', '');
hdnNeedToConfirm = document.getElementById('<%= Page.Master.FindControl("MainContent").FindControl("HdnNeedToConfirm").ClientID %>');
if (hdnNeedToConfirm != null)
needToConfirm = hdnNeedToConfirm.value;
else
needToConfirm = "False";
if (needToConfirm == "True")
window.open("SaveOrLose.aspx");
}
</script>
I have two __dopostback statements in the code above, however the postbacks are not actually happening until AFTER the rest of the code is. Which means that the if statement fails, so the Save form is never opened.
I'd appreciate any help you could give!!