Postback Ritalin has been getting a lot of search hits intended to find a button disable technique for full .NET postbacks. So, this example is for all of you searching for a non-AJAX solution.
The trick is to use the OnClientClick and UseSubmitBehavior properties of the button control. There are other methods, involving code on the server side to add attributes, but I think the simplicity of doing it this way is much more attractive:
<asp:Button runat="server" ID="BtnSubmit" OnClientClick="this.disabled = true; this.value = 'Submitting...';" UseSubmitBehavior="false" OnClick="BtnSubmit_Click" Text="Submit Me!" />
The one pitfall that comes with disabling a submit button on the client side is that it will cancel the browser’s submit, and thus the postback. Setting the UseSubmitBehavior property to false tells .NET to inject the necessary client script to fire the postback anyway, instead of relying on the browser’s form submission behavior. In this case, the code it injects would be:
This is added to the end of our OnClientClick code, giving us this rendered HTML:
<input type="button" name="BtnSubmit" onclick="this.disabled = true; this.value = 'Submitting...';__doPostBack('BtnSubmit','')" value="Submit Me!" id="BtnSubmit" />
This gives a nice button disable effect and processing text, while the postback completes.
9 Mentions Elsewhere
- deviceZero » Disable a button control during postback.
- Disable button onclientclick twice ASP.Net « Yasserzaid’s Weblog
- ASP.NET disable a button control during postback « Basketman's Blog
- Disable button click twice ASP.Net « JMJTwin.co.cc
- Postback after disabling a button (ASP.NET) « My Software Notes
- Prevent multiple form submits in ASP.NET « BRuud – Blog by Ruud
- Disable control after AsyncPostBack using jQuery « .Net'ers
- ביטול לחיצה כפולה על כפתור « asp.net
- Disable a button on click inside an UpdatePanel | Code and Calm