简体   繁体   中英

AutoPostback on dropDownList issue

I have this piece of code to check with user if it is ok to leave page without saving:

<script language="JavaScript">

window.onbeforeunload = !isPostBack && confirmExit;

function isPostBack() {
    return <%= Page.IsPostBack ? "true":"false" %>
}

function confirmExit() {
     return "Some lame question for user. :)";     
}
</script>

There is a problem with pages which have dropDownLists and I must have AutoPostBack set to true. AutoPostBack=true results in annoying popup every time. I've figured out how to check for postBack (thank you Google), but now I'm stuck.

There shouldn't be mistake, I have no idea when it comes to java script. I'm poor desktop programmer trying to learn something new.

window.onbeforeunload is expected to be a function and not a bool value. You might have intended this, or similar:

if(<%= Page.IsPostBack ? "true":"false" %>)
{
    window.onbeforeunload = function()
    {
        return "Some lame question for user. :)"; 
    };
}

BTW, another way to avoid the re-posting warning would be the post-redirect-get pattern (redirect to the same page that you posted to, from within the event handler on the server).

onbeforeunload should store a function, not a boolean value, because it's a callback.

You could do something like :

window.onbeforeunload = function(){
    if(!isPostBack && confirmExit) { 
       //leave page
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM