Disable button on form submission

前端 未结 17 1708
伪装坚强ぢ
伪装坚强ぢ 2020-12-01 00:13

I have a button that I would like to disable when the form submits to prevent the user submitting multiple times.

I have tried naively disabling the button with java

17条回答
  •  猫巷女王i
    2020-12-01 00:56

    Building on @rp.'s answer, I modified it to invoke the custom function and either submit and disable on success or "halt" on error:

    public static void DisableButtonOnClick(Button ButtonControl, string ClientFunction)
    {
        StringBuilder sb = new StringBuilder(128);
    
        if (!String.IsNullOrEmpty(ClientFunction))
        {
            sb.AppendFormat("if (typeof({0}) == 'function') {{ if ({0}()) {{ {1}; this.disabled=true; return true; }} else {{ return false; }} }};", ClientFunction, ButtonControl.Page.ClientScript.GetPostBackEventReference(ButtonControl, null));
        }
        else
        {
            sb.Append("return true;");
        }
    
        ButtonControl.Attributes.Add("onclick", sb.ToString());
    }
    

提交回复
热议问题