Solution to prevent multiple submissions of page buttons in Asp. net
- 2021-08-31 07:36:17
- OfStack
Background of the problem:
In the KPI classification rating assessment system at hand, the submission button of the page is made with LinkButton or Button. When a large number of users visit the site online at the same time, the application server is in a bad situation that CPU occupies 100%, the page will be stuck, and the users will click the Submit button continuously to submit repeatedly, resulting in a large number of duplicate data in the database.
In fact, even if the server does not crash and the Submit button is clicked quickly and frequently, the problem of repeated submission will occur.
Tried: 1) Set the Enabled property of the Submit button in the click event of the Submit button
Enabled = false
; This property setting is invalid until you get a new page, and the problem still exists;
2) js sets the submit button in the client event
disabled disabled = true
; At this time, the server-side event of the submit button will no longer be executed;
3) I also tried to pop up the div mask layer immediately after clicking the submit button, and the effect is not ideal, and the problem still exists;
4) Add an HTML button and hide the Submit button, click HTML button to submit, make HTML button unavailable in the client event of HTML button (prevent repeated submission), and then trigger the server event of Submit button, and HTML button will automatically become available after the page is posted back. This method is feasible. If there are shortcomings or better methods, I hope you will give me your advice.
Solutions:
1) Add an HTML button in addition to the submit button LinkButton (ID is lbtSave)
<input type="button" id="htmlSave" value=" Submit " onclick="SingleSubmit()" />
2) Hide lbtSave. Note that the Visible attribute of lbtSave cannot be set to False to realize hiding, otherwise the server event lbtSave_Click of lbtSave will not be triggered; Feasible method: use < div style="display:none;" > < /div > Include lbtSave for hiding or directly use # lbtSave {display: none;} To achieve hiding
3) < head > < /head > Add the js code to the tag as follows:
<script type="text/javascript">
function SingleSubmit()
{
document.getElementById("htmlSave").disabled = true;
document.getElementById("lbtSave").click();
}
</script>
4) Click htmlSave, execute SingleSubmit () method, make htmlSave unavailable (prevent repeated submission), trigger lbtSave_Click event
5) htmlSave becomes available automatically after the page is posted back, that is, after the lbtSave_Click event is executed.
In order to simulate the real situation, the thread sleep Thread. Sleep (5000) of 5s is added to the lbtSave_Click event; .