Question:
How to Prevent Repeated Execution of ASP.NET Button Click?

Here, the primary objective is to display a confirm dialog box. If the user selects "ok," we then enable the server-side button event code to execute (and we also want to stop allowing clicks until the server-side code has finished running).


Here is the code that will help you get what you want:

markup:

      <asp:Button ID="myButton" runat="server" Text="Click Me"

            OnClientClick="return showConfirmBox(this);"

            OnClick="myButton_Click"

            CssClass="btn" />


        <script>


            function showConfirmBox(btn) {

                if (confirm('Are you sure you want to proceed?')) {


                    $(btn).hide()   // hide the button, prevent future clicks

                    return true

                }

                // Prevent the button click event from executing immediately

                return false;

            }

        </script>


And code behind for this test is this:

Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load



End Sub


Protected Sub myButton_Click(sender As Object, e As EventArgs)


    ' fake 2 second delay

    System.Threading.Thread.Sleep(2000)



End Sub


Suggested blogs:

>>How can I replace this.$parent in composition API in Vuejs?

>>What is meant by progressive framework?

>>Why logged user object is all strings in Vuejs and Laravel 10?

>>How to get the date and time and display it in a defineProps in Vuejs?

>>Fix error while retrieving pages from a pdf using convert_from_path (pdf2image)

>>How .transform handle the splitted groups?

>>Can I use VS Code's launch config to run a specific python file?

>>Python: How to implement plain text to HTML converter?


Ritu Singh

Ritu Singh

Submit
0 Answers