ASP. NET Disable button to prevent duplicate commits
Developer on Alibaba Coud: Build your first app with APIs, SDKs, and tutorials on the Alibaba Cloud. Read more ＞
Add onclientclick= "This.disabled=true" only in the button foreground code; Usesubmitbehavior= "False".
protected void Page_Load (object sender, EventArgs e)
BUTTON1.ATTRIBUTES.ADD ("onclick", this.) GetPostBackEventReference (Button1) + "; this.disabled=true;");
Note: Do not write in if (! Page.IsPostBack), otherwise the script will be wrong.
The process is to click the button and set it to disabled, which is this.disabled=true.
Although the process is very simple, but there is a small trick (I did not know), is the button control is not used in the Usesubmitbehavior property, to make the button click after disabled, and the page (form) to commit, The Usesubmitbehavior must be set to false, otherwise the page (form) will not be submitted.
MSDN's explanation for Usesubmitbehavior is:
Use the Usesubmitbehavior property to specify whether the Button control uses the client browser's commit mechanism or the ASP. By default, this property has a value of true, which causes the Button control to use the browser's commit mechanism. If False is specified, the ASP. NET page framework adds client script to the page to send the form to the server.
When the Usesubmitbehavior property is false, the control developer can use the GetPostBackEventReference method to return the client postback event for the Button. The string returned by the GetPostBackEventReference method contains the text of the client function call that can be inserted into the client event handler.
<asp:button id= "Dopublishbutton" runat= "Server" text= "Publish" onclick= "Dopublishbutton_click"
height= "30px" onclientclick= "this.disabled=true;" Usesubmitbehavior= "False"/>
When Usesubmitbehavior is false, it will be seen in the output HTML.
__doPostBack (' Issue1$dopublishbutton ', ') is the ASP. If Usesubmitbehavior is true, there will be no such sentence, and the page (form) will not be submitted.
This article is an English version of an article which is originally in the Chinese language on aliyun.com and is provided for information purposes only. This website makes no representation or warranty of any kind, either expressed or implied, as to the accuracy, completeness ownership or
reliability of the article or any translations thereof. If you have any concerns or complaints relating to the article, please send an email, providing a detailed description of the concern or
complaint, to email@example.com. A staff member will contact you within 5 working days. Once verified, infringing content will be removed immediately.
The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion;
products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the
content of the page makes you feel confusing, please write us an email, we will handle the problem
within 5 days after receiving your email.
If you find any instances of plagiarism from the community, please send an email to:
and provide relevant evidence. A staff member will contact you within 5 working days.