Implement code that prevents multiple page submissions in ASP. NET

Source: Internet
Author: User

The Code provided here is used to disable all the buttons on the asp.net page after a Button is clicked, thus preventing multiple submissions due to submission delay. Based on the previous onceclickbutton script.

 
 
  1. // Code preventing multiple page submissions in ASP. NET: javascript
  2. < Script Language="Javascript"> 
  3.  <! --
  4. Function disableOtherSubmit ()
  5. {VarObj=Event. SrcElement;
  6. VarObjs=Document. GetElementsByTagName ('input ');
  7. For (varI=0; I< Objs. length; I ++)
  8. {
  9. If (objs [I]. type. toLowerCase () = 'submit ')
  10. {
  11. Objs [I]. Disabled=True;
  12. }
  13. }}
  14. //--> 
  15.  </Script>
 
 
  1. //Code Used in ASP. NET to prevent multiple page submissions: asp.net
  2. Public ClassPreventMultiClick: System. Web. UI. Page
  3. {
  4.  ProtectedSystem. Web. UI. WebControls. Button Button1;
  5.  ProtectedSystem. Web. UI. WebControls. Button Button2;
  6.  ProtectedSystem. Web. UI. WebControls. LinkButton LinkButton1;
  7.  ProtectedSystem. Web. UI. WebControls. Button Button3;
  8.  Private VoidPage_Load (ObjectSender, System. EventArgs e)
  9. {
  10.  This. GetPostBackEventReference (This. Button3 );// Ensure that _ doPostBack (eventTarget, eventArgument) is correctly registered 
  11.  If(! IsPostBack)
  12. {
  13. System. Text. StringBuilder sb =NewSystem. Text. StringBuilder ();
  14. Sb. Append ("If (typeof (Page_ClientValidate) = 'function') {if (Page_ClientValidate () = false) {return false ;}}");// Verify the execution of the Function 
  15. Sb. Append ("If (window. confirm ('are you sure? ') = False) return false ;");// Customize the client script 
  16. Sb. Append ("DisableOtherSubmit ();");// Disable all submit buttons 
  17. Sb. Append (This. GetPostBackEventReference (This. Button3 ));// Submit with _ doPostBack to ensure that the click Event of the button is executed on the server side. 
  18. Sb. Append (";");
  19. Button3.Attributes. Add ("Onclick", Sb. ToString ());
  20. }
  21. }
  22. # Region Web Form Designer generated code
  23.  Override Protected VoidOnInit (EventArgs e)
  24. {
  25.  // 
  26.  // CODEGEN: This call is required by the ASP. NET Web Form Designer. 
  27.  // 
  28. InitializeComponent ();
  29.  Base. OnInit (e );
  30. }
  31.  /// <Summary> 
  32.  /// Required method for Designer support-do not modify 
  33.  /// The contents of this method with the code editor. 
  34.  /// </Summary> 
  35.  Private VoidInitializeComponent ()
  36. {
  37.  This. Button3.Click + =NewSystem. EventHandler (This. Button3_Click );
  38.  This. Load + =NewSystem. EventHandler (This. Page_Load );
  39. }
  40. # Endregion
  41.  Private VoidButton3_Click (ObjectSender, System. EventArgs e)
  42. {
  43. System. Threading. Thread. Sleep (3000 );
  44. Response. Write ("Hello world! ");
  45. }
  46. }

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.

  1. ASP. NET Server Control Development-Composite Control
  2. Introduction to "three-layer structure" in ASP. NET
  3. 26 methods for optimizing ASP. NET performance
  4. Comparison of html controls and web controls in ASP. NET
  5. Object Description in ASP. NET

Related Article

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.