Prevents users from clicking the submit button to process code multiple times before the server completes processing.

Source: Internet
Author: User

If the webpage speed is too slow or the user submits multiple times, the data may be modified. How can this problem be solved?
This section is placed in Page_Load.
Copy codeThe Code is as follows:
If (! Page. IsPostBack)
{
System. Text. StringBuilder s = new System. Text. StringBuilder ();
S. Append ("();");
S. Append (this. GetPostBackEventReference (this. Button1 ));
This. Button1.Attributes. Add ("onclick", s. ToString ());
}
A () is JS
Function ()
{
Var OK = document. getElementById ('button1 ');
OK. disabled = true;
Return true;
}

After concentration:
Copy codeThe Code is as follows:
BtnSave. Attributes. Add ("onclick", "this. disabled = 'true';" + GetPostBackEventReference (btnSave ));

A problem was slightly plagued, and then solved, btnSave. attributes. add ("onclick", "a ();" + GetPostBackEventReference (btnSave); If a () contains other verifications, such as some regular expressions, btnSave. attributes. add ("onclick", "return a ();" + GetPostBackEventReference (btnSave. You can write all JS code in the CS file and then pull it.
Copy codeThe Code is as follows:
System. Text. StringBuilder s = new System. Text. StringBuilder (); s. Append ("var OK = document. getElementById ('button1 ');");
S. Append ("OK. disabled = true ;");
S. Append (this. GetPostBackEventReference (this. Button1 ));
This. Button1.Attributes. Add ("onclick", s. ToString ());
//. Net 2.0 or above
Button1.Attributes. Add ("onclick", "this. disabled = true;" + this. ClientScript. GetPostBackEventReference (Button1 ,""));

Or:
Copy codeThe Code is as follows:
<Asp: Button ID = "btnSumbit" runat = "server" UseSubmitBehavior = "false" OnClientClick = "this. value = 'sumbit '; this. disabled = true; "Text =" Sumbit "OnClick =" btnSumbit_Click "/>

Other methods (available for trial)
Method 1:
Copy codeThe Code is as follows:
Protected void Page_Load (object sender, EventArgs e)
{
Btn. Attributes. Add ("onclick", "state = true ;");
StringBuilder sb = new StringBuilder ();
Sb. Append ("if (! State) return ;");
Sb. Append ("var button = document. getElementByIdx_x ('btn ');");
Sb. Append ("button. value = 'Please Wait ...';");
Sb. Append ("document. body. style. cursor = 'wait ';");
Sb. Append ("button. disabled = true ;");
String strScript = "<script> ";
StrScript = strScript + "var state = false ;";
// Bind the function to the onbeforeunload event on the page:
StrScript = strScript + "window. attachEvent ('onbeforeunload', function () {" + sb. ToString () + "});";
StrScript = strScript + "</" + "script> ";
Page. RegisterStartupScript ("onbeforeunload", strScript );
}
Protected void Submit_Click (object sender, EventArgs e)
{
// Simulate button processing for a long time
System. Threading. Thread. Sleep (2000 );
Response. Write ("<script> alert ('bbbbbb !! '); "+" </"+" Script> ");
}
<Asp: Button ID = "btn" Text = "Submit" OnClick = "Submit_Click"
Runat = "server"/>

Method 2:
Copy codeThe Code is as follows:
<Asp: button id = "btnSubmit" OnClick = "Submit_Click" runat = "server" OnClientClick = "this. disabled = true; this. form. submit (); "UseSubmitBehavior =" False "/>

Method 3:
Copy codeThe Code is as follows:
This. btnSubmit. Attributes ["onclick"] = this. GetPostBackEventReference (this. btnSubmit) + "; this. disabled = true;"; // prevents repeated submissions

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.