Prevent sending messages in ASP.NET Web formats

I know this is the main question, but I'm curious what the different options are and what the best practice will be.

If I have a form that is responsible for saving the reservation in the system, how can I prevent the form from being published twice if the user double-clicks the button?

I know that there are several ways I can do this, but I'm not quite sure what is the standard way to prevent this. Partly because I'm new to web forms, and I'm used to working with MVC.

Thanks in advance.

+4
source share
5 answers

I used two approaches to this problem:

  • Use a token-based approach. Each page has a hidden input with the current random token. This token is also stored in the user session. When the postback occurs, I compare the tokens and, if valid, generates a new session token and continues processing. When the second reverse transfer occurs, the token no longer matches and does not allow processing.

  • Use javascript to disable the submit button. If you take this approach and you need a button event handler, you will need to create a hidden input with a button name attribute before submitting. Hidden input is necessary because disabled inputs do not fall into the message data.

+2
source

I would recommend a client-side onClick event handler that disables the button or makes it invisible, preferably the last, and replaces the button with a label that reads "Processing ..." or something like this

+1
source

I used something similar when using asp:Button to send:

1) Set the submit button UseSubmitBehavior="false"

2) Set the submit button OnClientClick="pleaseWait(this, 'Please Wait...');"

3) Include javascript code on the page:

 function pleaseWait(obj, message) { if (typeof(Page_ClientValidate) == 'function') { if (Page_ClientValidate()) { obj.disabled = true; obj.value = message; return true; } } return false; } 

This solution is good because it is simple, but still allows for client-side validation of javascript. This is not ideal, because it still relies on Javascript which can be turned off, but it is unlikely to be done by those who do not make sense to click once and wait for an answer. :)

+1
source

An easy way is to use the ajax AnimationExtender control. http://www.asp.net/ajaxLibrary/AjaxControlToolkitSampleSite/Animation/Animation.aspx

Just attach the expander to the button and add a disconnect action.

0
source
  <asp:Button ID="CopyScenarioButton" ClientIDMode="Static" OnClick="CopyScenarioButton_Click" OnClientClick="setTimeout( function() {$('#CopyScenarioButton').attr('disabled', 'disabled');},0)" EnableViewState="false" Text="Save New Scenario" ToolTip="Save New Scenario" CssClass="btnNormal" runat="server" /> 

or later, which includes some verification:

  function PreSaveHybrid() { var doSave = PreSave(); if (doSave !== false) //returns nothing if it not a cancel setTimeout(function () { $('#btnSave').attr('disabled', 'disabled'); }, 0); return doSave; } 
0
source

Source: https://habr.com/ru/post/1381187/


All Articles