How to tell if a page unload in ASP is a PostBack
<p>This seems like a common question but search is not returning anything.</p>

<p>I have the following code that executes before the page unloads.The problem is if the unload is a postback i do not want to fire my warning to the user but i can't figure out how to differentiate between a postback and a user navigating to another page for example.</p>

<pre><code>// This is executed before the page actually unloads
        $(window).bind("beforeunload", function () {

            if (prompt) {

                //prompt
                return true;
            }
            else {

                //reset our prompt variable
                prompt = true;
            }
        })
</code></pre>

<p>Running script in the code behind i.e. if Page.IsPostBack then set prompt is not an option.</p>

<p>Any ideas?</p>

<p>EDIT: </p>

<p>Here is the solution I ended up with:</p>

<pre><code> function DoNotPrompt() {
              prompt = false;
        }
</code></pre>

<p>I then added this to all the controls where the user could do something that result in a post back.</p>

<pre><code>OnClientClick="DoNotPrompt()
</code></pre>

<p>Then checked this flag and only returned a string in "beforeunload" if the user was really moving away from the page i.e. not a postback.</p>

<p>I also had to use this code:
var magicInput = document.getElementById('__EVENTTARGET');</p>

<pre><code>    if (magicInput &amp;&amp; magicInput.value) {
        // the page is being posted back by an ASP control 
        prompt = false;
    }
</code></pre>

<p>The reason being i had a custom user control that was a list box and I could not add the above method. So used this to catch that event and set the flag to false.</p>

<p>Not the most elegent solution.</p>

<p>Thanks,
Michael</p>
