Code implementation in ASP. NET to prevent multiple page submissions

Source: Internet
Author: User

Provided hereCodeIt is used to disable all the buttons on the Asp.net page after a button is clicked, thus preventing multiple submissions due to the submission delay. Based on the previous onceclickbutton script.

// Code preventing multiple page submissions in ASP. NET: javascript <script language = "JavaScript"> <! -- Function disableothersubmit (){
VaR OBJ = event. srcelement;
VaR objs = Document. getelementsbytagname ('input ');
For (VAR I = 0; I <objs. length; I ++)
{
If (objs [I]. type. tolowercase () = 'submit ')
{
Objs [I]. Disabled = true;
}
}
} // --> </SCRIPT> // code preventing multiple page submissions in ASP. NET: Asp. netpublic class preventmulticlick: system. Web. UI. Page {
Protected system. Web. UI. webcontrols. Button button1; protected system. Web. UI. webcontrols. Button button2;
Protected system. Web. UI. webcontrols. linkbutton linkbutton1; protected system. Web. UI. webcontrols. Button button3; private void page_load (Object sender, system. eventargs E)
{
This. getpostbackeventreference (this. button3 );
// Ensure that _ dopostback (eventtarget, eventargument) is correctly registered if (! Ispostback)
{
System. Text. stringbuilder sb = new system. Text. stringbuilder ();
SB. append ("If (typeof (page_clientvalidate) = 'function ')
{
If (page_clientvalidate () = false)
{
Return false;
}
} "); // Ensure that the execution of the verification function sb. append (" If (window. Confirm ('Are you sure? ') = False) return false ;");
// Customize the client script sb. append ("disableothersubmit ();");
// Disable all submit buttons sb. append (this. getpostbackeventreference (this. button3 ));
// Submit with _ dopostback to ensure that the server-side Click Event of the button executes sb. append (";");
Button3.attributes. Add ("onclick", SB. tostring ());
}
} # Region web form designer generated code override protected void oninit (eventargs E)
{
/// Codegen: This call is required by the ASP. NET web form designer. // initializecomponent ();
Base. oninit (E );
}
/// <Summary> /// required method for designer support-do not modify // the contents of this method with the code editor. /// </Summary> private void initializecomponent ()
{
This. button3.click + = new system. eventhandler (this. button3_click); this. Load + = new system. eventhandler (this. page_load );
}
# Endregion private void button3_click (Object sender, system. eventargs E)
{
System. Threading. thread. Sleep (3000 );
Response. Write ("Hello world! ");
}
} Here, only disable all the submit buttons. I think other submit controls can also be disable in a similar way.

The preceding code is implemented in ASP. NET to prevent multiple page 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.