Feb 26
2010

If a user tries to hit an ASP.NET submit button more than once only the very first request should submit to server.
All other clicks shouldn't make any request. So, for this you need to disable the button when first time click on button and enable it after processed.

Example 1)  in codebehind:
   
   /// <summary>
   /// OnInit stage
   /// </summary>
   /// <param name="e"></param>
   protected override void OnInit(EventArgs e)
   {
        base.OnInit(e);
        btnDataSave.Attributes.Add("onclick", "javascript:" + btnDataSave.ClientID + ".disabled=true;"  + ClientScript.GetPostBackEventReference(btnDataSave, string.Empty));
    }
  
  The method "ClientScript.GetPostBackEventReference" allows you to create the "__doPostBack Script" code which causes a serverside postback. 
  
  
  or example 2) in aspx:
  
            <asp:Button
                ID="btnDataSave"
                runat="server"        
                Text="SAVE"
                OnClientClick='this.disabled=Page_ClientValidate("valgrpData");'
                UseSubmitBehavior="false"
                ToolTip="save data"
                ValidationGroup="valgrpData" />
      
   using this js function:
   
   //check if clientside validation of a single validationgroup is valid
   //returns a boolean value
   function Page_ClientValidate(valGrp)
   {
      Page_InvalidControlToBeFocused = null;
      if (typeof(Page_Validators) == "undefined")
      {
        return true;
      }
      var i;
      for (i = 0; i < Page_Validators.length; i++)
      {
         ValidatorValidate(Page_Validators[i], valGrp, null);
      }
      ValidatorUpdateIsValid();
      ValidationSummaryOnSubmit(valGrp);
      Page_BlockSubmit = !Page_IsValid;
      return Page_IsValid;
   }
      
The button will only be disabled when page validation passed successful.   
   
Problem when using both ways:
In Firefox the disabled button looks the same like an enabled button, in IE it works.
This could irritate the user. Well this is a different behaviour of browsers.
But if necessary there may be more complex ways to solve this problem for example
using different styles or images for the button.

    
Thanxs for Praveen Kumar Battula for his initial idea i found at
http://praveenbattula.blogspot.com/2010/01/disable-button-in-onclick-and-process.html

Tags:

Related posts

Comments

mini projector

Posted on Monday, 1 March 2010 03:58

Thank you for your post. I agree with your idea. Great post !

flea

Posted on Tuesday, 2 March 2010 07:29

Avoid to submit more than once by clicking a button. Thank you for another great article. Where else could anyone get that kind of information in such a perfect way of writing? I have a presentation next week, and I am on the look for such information..

Comments are closed