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) |
03 |
System. Text. stringbuilder clientsideeventreference =New System. Text. stringbuilder (); |
05 |
// Whether to implement page Verification |
06 |
If (((This. Page! =Null)&&This. Causesvalidation )&&(This. Page. validators. Count> 0 )) |
08 |
Clientsideeventreference. append ("If (typeof (page_clientvalidate) = 'function') {If (page_clientvalidate () = false) {return false ;}}"); |
11 |
// Prompt for submission |
12 |
If (This. Showmessagebox) |
14 |
Clientsideeventreference. append ("If (! Confirm ('" +This. Warningtext +"') {Return false }"); |
17 |
// Whether to use the onclientclick attribute of the button for JS Verification |
18 |
If (This. Onclientclick! ="") |
20 |
Clientsideeventreference. append ("If (! "+ Onclientclick +") {Return false ;}"); |
23 |
Clientsideeventreference. appendformat ("This. value = '{0 }';",(String)This. Viewstate ["Aftersubmittext"]); |
24 |
Clientsideeventreference. append ("This. Disabled = true ;"); |
27 |
Clientsideeventreference. append (This. Page. clientscript. getpostbackeventreference (This,String. Empty )); |
29 |
Writer. addattribute (htmltextwriterattribute. onclick, clientsideeventreference. tostring (),True); |
30 |
Base. Addattributestorender (writer ); |
Use the Custom button sample core code:
View Source
Print?
01 |
<Pre Class=Brush: CSHARP> <Script Type="Text/JavaScript"> |
04 |
VaR TXT = Document. getelementbyid ('<% = txtcontext. clientid %> '); |
07 |
Alert ('text box cannot be blank! '); |
15 |
cc1: clickoncebutton id = " btnadd " runat = "server" text = "Submit" onclick = "btnadd_click" onclientclick = " Validate () " /> |
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