Disable Submit button in case of multiple click.. (C#)

后端 未结 6 904
一向
一向 2021-01-01 05:55

My Problem is ,I have a simple web form, which contains two textboxes and a button.there are some asp.net validator controls on page.so i want client side disabling of butto

6条回答
  •  清酒与你
    2021-01-01 06:19

    If you disable the button then form submission will not happen. Correct way would be to set timer to disable the button. I would also suggest to use submit behavior instead of putting post-back event ref. For example,

    function clickHandler(id, validate, validationGroup) {
       var isValid = true;
       if (validate && typeof(Page_ClientValidate) == 'function') {
         isValid = validationGroup? Page_ClientValidate(validationGroup): Page_ClientValidate();
       }
       if (isValid)
       {
          // set timer to disable the button
          var b = document.getElementById(id);
          var f = function() { b.disabled = 'disabled'; };
          setTimeout(f, 100);
          return true;
       }
       return false;
    }
    

    And now attach function to your button

    protected override void OnPreRender(EventArgs e)
    {
        this.Attributes.Add("onclick", 
           string.Format("return clickHandler('{0}', {1}, '{2}')", 
           this.ClientID, this.CausesValidation ? "true" : "false", 
           this.ValidationGroup));
    }
    

提交回复
热议问题