Prevents users from clicking the submit button multiple times to process code before the server process is complete

  • 2020-05-19 04:34:41
  • OfStack

If the page speed is too slow or other reasons, the user submitted many times can lead to the modification of the data, how to solve this problem?
This is in Page_Load

 
if(!Page.IsPostBack) 
{ 
System.Text.StringBuilder s = new System.Text.StringBuilder(); 
s.Append("a();"); 
s.Append(this.GetPostBackEventReference(this.Button1)); 
this.Button1.Attributes.Add("onclick",s.ToString()); 
} 
a()  is  JS 
function a() 
{ 
var ok=document.getElementById('Button1'); 
ok.disabled = true; 
return true; 
} 

After concentration, it is:
 
btnSave.Attributes.Add("onclick","this.disabled='true';"+GetPostBackEventReference(btnSave)); 

One problem bothered me a little bit for 1, and then it was solved, btnSave.Attributes.Add ("onclick","a();" + GetPostBackEventReference (btnSave)); btnSave.Attributes.Add ("onclick","return a();" + GetPostBackEventReference (btnSave)); You can't do it. You can write the JS code in the CS file as OK pull.
 
System.Text.StringBuilder s = new System.Text.StringBuilder(); s.Append("var ok=document.getElementById('Button1'); "); 
s.Append("ok.disabled = true; "); 
s.Append(this.GetPostBackEventReference(this.Button1)); 
this.Button1.Attributes.Add("onclick",s.ToString()); 
//.net 2.0 The above  
Button1.Attributes.Add("onclick", "this.disabled=true;" + this.ClientScript.GetPostBackEventReference(Button1, "")); 

Or:
 
<asp:Button ID="btnSumbit" runat="server" UseSubmitBehavior="false" OnClientClick="this.value='Sumbit';this.disabled=true; " Text="Sumbit" OnClick="btnSumbit_Click" /> 

Other methods (to try)
Method 1:
 
protected void Page_Load(object sender, EventArgs e) 
{ 
btn.Attributes.Add("onclick", "state=true;"); 
StringBuilder sb = new StringBuilder(); 
sb.Append("if (!state) return;"); 
sb.Append("var button=document.getElementByIdx_x('btn');"); 
sb.Append("button.value='Please Wait...';"); 
sb.Append("document.body.style.cursor='wait';"); 
sb.Append("button.disabled=true;"); 
string strScript = "<script>"; 
strScript = strScript + "var state=false;"; 
// Binding a function to a page onbeforeunload The event : 
strScript = strScript + "window.attachEvent('onbeforeunload',function(){" + sb.ToString() + "});"; 
strScript = strScript + "</" + "script>"; 
Page.RegisterStartupScript("onbeforeunload", strScript); 
} 
protected void Submit_Click(object sender, EventArgs e) 
{ 
// Simulate long time button handling  
System.Threading.Thread.Sleep(2000); 
Response.Write("<script>alert('bbbbbb!!');" + "</" + "script>"); 
} 
<asp:Button ID="btn" Text="Submit" OnClick="Submit_Click" 
runat="server"/> 

Method 2:
 
<asp:button id="btnSubmit" OnClick="Submit_Click" runat="server" OnClientClick="this.disabled=true;this.form.submit();" UseSubmitBehavior="False"/> 

Method 3:
 
this.btnSubmit.Attributes["onclick"]=this.GetPostBackEventReference(this.btnSubmit)+";this.disabled=true;";// Prevent duplicate submissions  


Related articles: