The button becomes grayed out after the button is submitted

Source: Internet
Author: User

To prevent multiple clicks, the click button turns gray. The Code is as follows: (C #)

Private void page_load (Object sender, system. eventargs E)
...{
// Place user code here to initialize the page
The button becomes grayed out after the button is submitted
String script = This. getpostbackeventreference (this. user_botton );
If (! Page. ispostback)
...{
System. Text. stringbuilder sb = new system. Text. stringbuilder ();
SB. append ("your temporary Doc ument. getelementbyid ('" + this. user_botton.clientid + "'). Disabled = true ;");
SB. append (SCRIPT );
SB. append (";");
This. user_botton.attributes.add ("onclick", SB. tostring ());
}
// End
}

 

Public void page_load (Object OBJ, eventargs E)
...{
BTN. Attributes. Add ("onclick", "State = true ;");
Stringbuilder sb = new stringbuilder ();
SB. append ("If (! State) return ;");
SB. append ("Var button = Document. getelementbyid ('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 );
}

Private void submit_click (Object sender, eventargs e )...{
// Simulate button processing for a long time
System. Threading. thread. Sleep (3000 );
Response. Write ("<SCRIPT> alert (XXX !! '); "+" </"+" Script> ");

}

<Asp: button id = "BTN" text = "Submit" onclick = "submit_click" runat = "server"/>

The modified example is to put the button in an invalid state for a certain period of time after submission.
It can only prevent the accidental clicks caused by users who are impatient due to slow network speeds.
There is still no solution for multiple submissions formed by malicious refresh.
(However, the onbeforeunload event on the page should be used to solve the problem)

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.