Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I have a button be disabled with jQuery but also run server code once it's been clicked?

Tags:

jquery

c#

asp.net

I have a button that will execute a stored procedure. After it's been clicked, I want it disabled so that the user can't click it multiple times. However, once it's been clicked, I also want to run some C# on the server side through an onclick event. As of right now, all my code does is disable the button, but then it won't run the server side stuff. Can this be achieved? Here's my jQuery:

 $("#btnGenerate").click(function () {
        $(this).attr("disabled", true).val("Generating...");
    });
like image 288
The Vanilla Thrilla Avatar asked Dec 31 '25 12:12

The Vanilla Thrilla


1 Answers

You can make an ajax call to the server just after you hide/disable the button.

You can do something like this:

$("#btnGenerate").click(function () {  
    $(this).attr("disabled", true).val("Generating...");  
    // an ajax call to the server to call the stored procedure
    $.ajax({
         url: "CallStoredProcedure.aspx",
         context: $("#value"),
         success: function(){
                $(this).val("done!");
           }});  
    }
like image 195
Sofian Hnaide Avatar answered Jan 02 '26 04:01

Sofian Hnaide