ASP. NET Disable button to prevent duplicate commits

Source: Internet
Author: User
Method One:

Add onclientclick= "This.disabled=true" only in the button foreground code; Usesubmitbehavior= "False".

Method Two:
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.

For example
<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.
<input type= "button" Name= "Issue1:dopublishbutton" value= "release" onclick= "This.disabled=true;__dopostback (' Issue1$ Dopublishbutton ', ') "language=" JavaScript "id=" Issue1_dopublishbutton "style=" height:30px; "/>


__doPostBack (' Issue1$dopublishbutton ', ') is the ASP. If Usesubmitbehavior is true, there will be no such sentence, and the page (form) will not be submitted.
  • Related Article

    Contact Us

    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: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.