title: How to prevent AutoPostBack when DropDownlist is selected using jQuery
body: <p>I want to show a confirm dialog when the user selects an item in a <code>DropDownList</code>. If the user presses <kbd>Cancel</kbd>, I want to stop the postback. Here is the function I add to the <code>onchange</code> event:</p>
  <pre><code>function imitateConfirmUnload(event) {
      if (window.onbeforeunload = null)
          return true;
  
      return confirm(&quot;Are you sure you want to navigate away from this page?\n\nYou have unsaved changes\n\nPress OK to continue or Cancel to stay on the current page.&quot;);
  }
  </code></pre>
  <p>And this is the relevant bit of code in my startup script to add the handler to the event:</p>
  <pre><code>function addConfirmToWarnClasses() {
  
      $('.warn').change(function() {
          imitateConfirmUnload()
      });
  }
  </code></pre>
  <p>The problem is that the postback occurs even if the user selects <kbd>Cancel</kbd>. If I move the handler on to the click event, it works. But it feels clunky to me.</p>
  <p><strong>Edit</strong></p>
  <p>Correction: it doesn't work <code>onclick</code>, because the dialog prevents selection, so when the user selects OK, no change has taken place, and no postback when you want it!</p>
  <p><strong>Edit 2</strong></p>
  <p>The <code>DropDownList</code> is inside an <code>UpdatePanel</code> so that may affect behaviour.</p>