Home  >  Article  >  Backend Development  >  asp.net disable button to prevent duplicate submissions

asp.net disable button to prevent duplicate submissions

巴扎黑
巴扎黑Original
2016-12-20 15:52:391765browse

Method 1:

Just add OnClientClick="this.disabled=true;" UseSubmitBehavior="False" to the button front code.

Method 2:
protected void Page_Load(object sender, EventArgs e)
{
 Button1.Attributes.Add("onclick", this.GetPostBackEventReference(Button1) + ";this.disabled=true;");
}
Note: Do not write it in if (!Page.IsPostBack), otherwise a script error will occur.



The process is to set the button to disabled after clicking it, that is, this.disabled=true.
Although the process is very simple, there is a little trick (I didn’t know it before), which is the UseSubmitBehavior attribute that is not commonly used in the Button control. To make the Button disabled after clicking and the page (form) still needs to be submitted, you must UseSubmitBehavior is set to false, otherwise the page (form) will not be submitted.

MSDN’s explanation of UseSubmitBehavior is:

Use the UseSubmitBehavior attribute to specify whether the Button control uses the client browser’s submission mechanism or the ASP.NET postback mechanism. By default, the value of this property is true, causing the Button control to use the browser's submission 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, control developers can use the GetPostBackEventReference method to return Button's client postback event. The string returned by the GetPostBackEventReference method contains the text of the client function call and can be inserted into a client event handler.

For example
Height="30px" OnClientClick="this.disabled=true;" UseSubmitBehavior="False" />


When UseSubmitBehavior is false, you will see it in the output Html



__doPostBack('Issue1$doPublishButton',''), which is asp .net added. If UseSubmitBehavior is true, there will be no such sentence and the page (form) will not be submitted.

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn