. Net to Prevent Users From repeatedly pressing the button

Source: Internet
Author: User

On the Internet, we often interact with users through table lists.
However, because the Web response is slow and fast, the table information may be sent out, resulting in a duplicate loading of the data.
Asp.net has a small way to defend against such situations.

Protected void page_load (Object sender, eventargs E)
...{
If (! Page. ispostback)
...{
Button1.attributes ["onclick"] = "This. Disabled = true;" +
Page. clientscript. getpostbackeventreference (button1 ,"");
}
}
Protected void button#click (Object sender, eventargs E)
...{
System. Threading. thread. Sleep (5000 );
}

When page load is performed, add an onclick attribute to the button. After you press the button, disable yourself and then issue the form submit statement.

<Div>
<Input type = "hidden" name = "_ eventtarget" id = "_ eventtarget" value = ""/>
<Input type = "hidden" name = "_ eventargument" id = "_ eventargument" value = ""/>
<Input type = "hidden" name = "_ viewstate" id = "_ viewstate" value = "/comment + j8gjxshabhpv4 ="/>
</Div>

<SCRIPT type = "text/JavaScript">
<! --
VaR theform = Document. Forms ['form1'];
If (! Theform ){
Theform = Document. form1;
}
Function _ dopostback (eventtarget, eventargument ){
If (! Theform. onsubmit | (theform. onsubmit ()! = False )){
Theform. _ eventtarget. value = eventtarget;
Theform. _ eventargument. value = eventargument;
Theform. Submit ();
}
}
// -->
</SCRIPT>

<Div>
<Input type = "Submit" name = "button1" value = "button" onclick = "this. disabled = true ;__ dopostback ('button1', '');" id = "button1"/>
</Div>

<Div>

In this way, the user can avoid re-clicking the button.

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.