title: Display message if close window in ASP.NET but not on PostBack
body: <p>I would like to display a message to the user only if they close my ASP.NET Web Forms page or navigate away from it. If they click any Button, LinkButton, AutoPostBack element, or anything else that will postback then I don't want to show the message.</p>
  
  <p>So far I have the following code:</p>
  
  <pre><code>&lt;script type="text/javascript"&gt;
  
  var postback = false;
  
  addToPostBack = function(func) {
      var old__doPostBack = __doPostBack;
      if (typeof __doPostBack != "function") {
          __doPostBack = func;
      } else {
          __doPostBack = function(t, a) {
              if (func(t, a)) old__doPostBack(t, a);
          }
      }
  };
  
  $(document).ready(function() {
      addToPostBack(function(t,a) {
          postback = true;
      });
  });
  
  $(window).bind("beforeunload", function() {
      if (!postback) {
          return "message";
      }
  });
  &lt;/script&gt;
  </code></pre>
  
  <p>This works partially but seems to stop AutoPostBack events from firing and still shows the message for LinkButtons etc.</p>
  
  <p>How can I do this?</p>