Thursday, February 24, 2011

Disable ASP:Button onclick

A common requirement is to disable a button after it's been clicked. It can be accomplished by modifying two properties of the asp:button control. First, set the UseSubmitButton to "false". Second, modify the OnClientClick property to include "this.disabled=true;". These two settings will work in most cases. A problem will crop up if client side data validation is being used. The button will remain disabled and the user will not be able to click the button again. If you're using .Net validation controls you will need to check Page_ClientValidate() and then disable the button. 

<script type='text/javascript'>
   function disableBtn(control) {
     if (typeof Page_ClientValidate == 'function') {
        if (Page_ClientValidate()) { control.disabled = true; }
     { control.disabled = true; }

<asp:Button runat="server" ID="btnSubmit" OnClientClick="disableBtn(this);" UseSubmitBehavior="false" OnClick="btnSubmit_Click" Text="Submit" />

No comments:

Post a Comment