title: Disable buttons on post back using jquery in .net app
body: <p>I have a asp.net app that I want to disable the buttons as soon as they are clicked in order to prevent multiple submissions.  I'd like to use jquery for this as the site already liberally uses it anyway.</p>
  
  <p>What I've tried is:</p>
  
  <pre><code>$(document).ready(function () {
      $("#aspnetForm").submit(function () {
          $('input[type=submit]', $(this)).attr("disabled", "disabled");
      })
  });
  </code></pre>
  
  <p>The above will disable the button, and the page submits, but the asp.net button on click handler is never called.  Simply removing the above and the buttons work as normal.</p>
  
  <p>Is there a better way?  Or, rather, what am I doing wrong?</p>
  
  <p>UPDATE
  Okay, I finally had a little time to put a very simple page together.</p>
  
  <pre><code>&lt;%@ Page Language="C#" AutoEventWireup="true" CodeBehind="SubTest.aspx.cs" Inherits="MyTesting.SubTest" %&gt;
  
  &lt;!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"&gt;
  
  &lt;html xmlns="http://www.w3.org/1999/xhtml"&gt;
  &lt;head runat="server"&gt;
      &lt;title&gt;&lt;/title&gt;
      &lt;script src="Scripts/jquery-1.4.1.min.js" type="text/javascript"&gt;&lt;/script&gt;
      &lt;script type="text/javascript"&gt;
          $(function () {
              $("#form1").submit(function () {
                  $('input[type=submit]', $(this)).attr("disabled", "disabled");
              });
          });
      &lt;/script&gt;
  &lt;/head&gt;
  &lt;body&gt;
      &lt;form id="form1" runat="server"&gt;
      &lt;div&gt;
          &lt;asp:Button ID="Button1" runat="server" onclick="Button1_Click" Text="Button" /&gt;
          &lt;asp:Button ID="Button2" runat="server" onclick="Button2_Click" Text="Button 2" /&gt;
      &lt;/div&gt;
      &lt;/form&gt;
  &lt;/body&gt;
  &lt;/html&gt;
  </code></pre>
  
  <p>The code behind looks like:</p>
  
  <pre><code>using System;
  
  namespace MyTesting {
      public partial class SubTest : System.Web.UI.Page {
      protected void Page_Load(object sender, EventArgs e) {
          if (IsPostBack) {
              // this will execute when any button is pressed
              Response.Write("postback");
          }
      }
          protected void Button1_Click(object sender, EventArgs e) {
          // never executes
              System.Threading.Thread.Sleep(1000);
              Response.Write("Button 1 clicked&lt;br /&gt;");
          } // method::Button1_Click
  
          protected void Button2_Click(object sender, EventArgs e) {
          // never executes
              System.Threading.Thread.Sleep(1000);
              Response.Write("Button 2 clicked&lt;br /&gt;");
          } // method::Button2_Click
      }
  }
  </code></pre>
  
  <p>When you click on a button it obviously disables the buttons, but NEITHER of the button clicks are run.</p>
  
  <p><strong>Rendered HTML</strong> </p>
  
  <pre><code>&lt;!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"&gt;
  
  &lt;html xmlns="http://www.w3.org/1999/xhtml"&gt;
  &lt;head&gt;&lt;title&gt;
  
  &lt;/title&gt;
      &lt;script src="Scripts/jquery-1.4.1.min.js" type="text/javascript"&gt;&lt;/script&gt;
      &lt;script type="text/javascript"&gt;
          $(function () {
              $("#form1").submit(function () {
                  $('input[type=submit]', $(this)).attr("disabled", "disabled");
              });
          });
      &lt;/script&gt;
  &lt;/head&gt;
  &lt;body&gt;
      &lt;form name="form1" method="post" action="SubTest.aspx" id="form1"&gt;
  &lt;div&gt;
  &lt;input type="hidden" name="__VIEWSTATE" id="__VIEWSTATE" value="/wEPDwUKMTcxODU4OTc0MWRkParC5rVFUblFs8AkhNMEtFAWlU4=" /&gt;
  &lt;/div&gt;
  
  &lt;div&gt;
  
      &lt;input type="hidden" name="__EVENTVALIDATION" id="__EVENTVALIDATION" value="/wEWAwKB57WhCAKM54rGBgK7q7GGCC6LlWKFoij9FIBVuI0HOVju/fTy" /&gt;
  &lt;/div&gt;
      &lt;div&gt;
          &lt;input type="submit" name="Button1" value="Button" id="Button1" /&gt;
          &lt;input type="submit" name="Button2" value="Button 2" id="Button2" /&gt;
      &lt;/div&gt;
      &lt;/form&gt;
  &lt;/body&gt;
  &lt;/html&gt;
  </code></pre>