Deka
Deka

Reputation: 131

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.

Upvotes: 0

Views: 847

Answers (2)

Eric Frick
Eric Frick

Reputation: 857

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
    }
}

Upvotes: 1

user191966
user191966

Reputation:

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).

Upvotes: 1

Related Questions