locked
Can double-click on a button be disabled RRS feed

  • Question

  • User1016820894 posted

    I have a form where the user clicks Save to write data to a sql database. In some isolated cases the user is double-clicking the button therefore it is writing the data out to the database twice. Is there any way to prevent a double click on a button?

    <asp:Button ID="SaveDataButton"  runat="server" Text="Save" Font-Size="14pt" Height="35px" Width="75px" onclick="SaveData_Click"/

    Friday, July 12, 2019 8:31 PM

All replies

  • User-943250815 posted

    You can disable button, popup an alert to confirm Save, or even cover page with a transparent div.
    Anyway, make sure user gets a Save confirmation, other than no matter waht you do, he/she will continue clicking Save button........

    <asp:Button ID="btnSave" runat="server" Text="Save"
       UseSubmitBehavior="false"
       OnClientClick="this.disabled='true';"
    onclick="SaveData_Click" </asp:button>

    Friday, July 12, 2019 9:15 PM
  • User288213138 posted

    Hi baldwinjohn,

    You can try to  use the window.onbeforeunload event, the script disables the ASP.Net Button as soon as the page is ready to do a PostBack including validation or the ASP.Net form is submitted.

    The code:

    <script type="text/javascript">
            function DisableButton() {
                document.getElementById("<%=Button1.ClientID %>").disabled = true;
            }
            window.onbeforeunload = DisableButton;
        </script>
    <div> <asp:Button ID="Button1" runat="server" Text="Button" OnClick="Button1_Click" /> </div>
    protected void Button1_Click(object sender, EventArgs e) { System.Threading.Thread.Sleep(2000); }

    The result:

    Best regards,

    Sam

    Monday, July 15, 2019 9:41 AM