If the webpage speed is too slow or the user submits multiple times, the data may be modified. How can this problem be solved?
This section is placed in Page_Load.
Copy codeThe Code is as follows:
If (! Page. IsPostBack)
{
System. Text. StringBuilder s = new System. Text. StringBuilder ();
S. Append ("();");
S. Append (this. GetPostBackEventReference (this. Button1 ));
This. Button1.Attributes. Add ("onclick", s. ToString ());
}
A () is JS
Function ()
{
Var OK = document. getElementById ('button1 ');
OK. disabled = true;
Return true;
}
After concentration:
Copy codeThe Code is as follows:
BtnSave. Attributes. Add ("onclick", "this. disabled = 'true';" + GetPostBackEventReference (btnSave ));
A problem was slightly plagued, and then solved, btnSave. attributes. add ("onclick", "a ();" + GetPostBackEventReference (btnSave); If a () contains other verifications, such as some regular expressions, btnSave. attributes. add ("onclick", "return a ();" + GetPostBackEventReference (btnSave. You can write all JS code in the CS file and then pull it.
Copy codeThe Code is as follows:
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 or above
Button1.Attributes. Add ("onclick", "this. disabled = true;" + this. ClientScript. GetPostBackEventReference (Button1 ,""));
Or:
Copy codeThe Code is as follows:
<Asp: Button ID = "btnSumbit" runat = "server" UseSubmitBehavior = "false" OnClientClick = "this. value = 'sumbit '; this. disabled = true; "Text =" Sumbit "OnClick =" btnSumbit_Click "/>
Other methods (available for trial)
Method 1:
Copy codeThe Code is as follows:
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 ;";
// Bind the function to the onbeforeunload event on the page:
StrScript = strScript + "window. attachEvent ('onbeforeunload', function () {" + sb. ToString () + "});";
StrScript = strScript + "</" + "script> ";
Page. RegisterStartupScript ("onbeforeunload", strScript );
}
Protected void Submit_Click (object sender, EventArgs e)
{
// Simulate button processing for a long time
System. Threading. Thread. Sleep (2000 );
Response. Write ("<script> alert ('bbbbbb !! '); "+" </"+" Script> ");
}
<Asp: Button ID = "btn" Text = "Submit" OnClick = "Submit_Click"
Runat = "server"/>
Method 2:
Copy codeThe Code is as follows:
<Asp: button id = "btnSubmit" OnClick = "Submit_Click" runat = "server" OnClientClick = "this. disabled = true; this. form. submit (); "UseSubmitBehavior =" False "/>
Method 3:
Copy codeThe Code is as follows:
This. btnSubmit. Attributes ["onclick"] = this. GetPostBackEventReference (this. btnSubmit) + "; this. disabled = true;"; // prevents repeated submissions