How to prevent repeated data submission when registering or clicking the button (instance description)

Source: Internet
Author: User

Asp.net implementation click the button to set the button to unavailable and submit
Copy codeThe Code is as follows:
<Asp: Button ID = "Button1" runat = "server" Text = "123456" OnClientClick = "this. disabled = true; this. form. submit (); "UseSubmitBehavior =" False "onclick =" button#click "/>

When the client is loaded, restart the operation.
Copy codeThe Code is as follows:
<Script language = "javascript" type = "text/javascript">
Function controlButton (flag ){
Var btnObj = document. getElementById ("Button1 ");
BtnObj. disabled = flag;
}

</Script>
</Head>
<Body onload = "controlButton (false);">

Background code:
Copy codeThe Code is as follows:
Protected void button#click (object sender, EventArgs e)
{
Thread. Sleep (3000 );
Response. Write ("123213 <br> ");
Response. Write ("123213 <br> ");
Response. Write ("123213 <br> ");
}

If there are many buttons on the page, you can use another method:

Background page_load code:
Copy codeThe Code is as follows:
This. BtnSend. Attributes. Add ("onclick", this. GetPostBackEventReference (this. BtnSend) + "; this. disabled = true ;");

Code of the front-end button:
Copy codeThe Code is as follows:
<Asp: button id = "BtnSend" runat = "server" Width = "80px" ForeColor = "White"
Text = "send" BackColor = "#376091" BorderColor = "# FFFFFF" Font-Bold = "True"
Style = "height: 24px" Font-Size = "13px" BorderStyle = "None" OnClick = "BtnSend_Click"> </asp: button>

Because the control's this. after disabled is set, the control is disabled and the background event method cannot be called at the sending back. Therefore, you must use GetPostBackEventReference to call the event Method in the background before calling disabled = true.

ASP. NET UseSubmitBehavior attributes

Definition and usage
UseSubmitBehavior attribute specifies whether the button control uses the submitted function built in the client browser or the postback mechanism of ASP. NET.

This property is set to TRUE if the control uses the browser's submission mechanism. Otherwise, the value is FALSE. The default value is TRUE.

When set to FALSE, ASP. NET adds a client script to upload the form back and forth.

When UseSubmitBehavior is set to false, the control developer can use the GetPostBackEventReference method to return the client sending event of the Button. The string returned by the GetPostBackEventReference method contains the text of the client function call and can be inserted into the client event handler.

Syntax
<Asp: Button UseSubmitBehavior = "TRUE | FALSE" runat = "server"/> instance
The following example uses the postback mechanism of ASP. NET:
Copy codeThe Code is as follows:
<Script runat = "server">
Sub SubmitBtn (obj As Object, e As EventArgs)
LblMsg. Text = "Submitted using the ASP. NET postback mechanic ."
End Sub
</Script>

<Form runat = "server">
Click the button:
<Asp: button id = "Button1" runat = "server"
Text = "Submit" onclick = "SubmitBtn"
UseSubmitBehavior = "FALSE"/>
<Br/>

<Asp: label id = "lblMsg" runat = "server"/>
</Form>

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.