Custom buttons prevent repeated submission

Source: Internet
Author: User

Due to network speed problems, users can't wait to click the submit button multiple times, resulting in multiple submissions. You can use the Custom button control to solve this problem.

Principle: Click the button and set its disabled attribute to true.

Custom button control coreCode:

View Source

Print?

01 Protected Override Void Addattributestorender (htmltextwriter writer)
02 {
03 System. Text. stringbuilder clientsideeventreference =New System. Text. stringbuilder ();
04   
05 // Whether to implement page Verification
06 If (((This. Page! =Null)&&This. Causesvalidation )&&(This. Page. validators. Count> 0 ))
07 {
08 Clientsideeventreference. append ("If (typeof (page_clientvalidate) = 'function') {If (page_clientvalidate () = false) {return false ;}}");
09 }
10   
11 // Prompt for submission
12 If (This. Showmessagebox)
13 {
14 Clientsideeventreference. append ("If (! Confirm ('" +This. Warningtext +"') {Return false }");
15 }
16   
17 // Whether to use the onclientclick attribute of the button for JS Verification
18 If (This. Onclientclick! ="")
19 {
20 Clientsideeventreference. append ("If (! "+ Onclientclick +") {Return false ;}");
21 }
22   
23 Clientsideeventreference. appendformat ("This. value = '{0 }';",(String)This. Viewstate ["Aftersubmittext"]);
24 Clientsideeventreference. append ("This. Disabled = true ;");
25  
26  
27 Clientsideeventreference. append (This. Page. clientscript. getpostbackeventreference (This,String. Empty ));
28   
29 Writer. addattribute (htmltextwriterattribute. onclick, clientsideeventreference. tostring (),True);
30 Base. Addattributestorender (writer );
31 }

 Use the Custom button sample core code:

View Source

Print?

01 <Pre Class=Brush: CSHARP> <Script Type="Text/JavaScript">
02 Function validate ()
03 {
04 VaR TXT = Document. getelementbyid ('<% = txtcontext. clientid %> ');
05 If (txt. value = "")
06 {
07 Alert ('text box cannot be blank! ');
08 TXT. Focus ();
09 Return false;
10 }
11  
12 Return true;
13 }
14 </Script>
15 cc1: clickoncebutton id = " btnadd " runat = "server" text = "Submit" onclick = "btnadd_click" onclientclick = " Validate () " />
16 </Pre>
17 <Strong> Notes </Strong>: If you set the onclientclick attribute for verification, the attribute value corresponding to the onclientclick attribute is: JS functions cannot carry the symbol ";" <BR> Example: incorrect syntax: onclientclick = "Validate ();" Correct syntax: onclientclick = "Validate ()"

Weaknesses in the US: The source code of the page shows two onclick events (as shown below). The onclick event corresponding to the onclientclick attribute does not play a role. Hope that the experts who pass by will give a solution!

View Source

Print?

1 <Input onclick ="If (! Validate () {return false;} This. value = 'submitting. Please wait... '; this. disabled = true ;__ dopostback ('btnadd ','')" Type ="Submit" Name ="Btnadd" Value ="Submit" Onclick ="Validate ();" Id ="Btnadd" />

Source code download

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.