-4

Without using Ajax, how can I disable a button while ExecuteSqlCommand is running? I don't want a user clicking the button multiple times before an execution finishes

EDIT

The web app is ASP.Net.

HTML:

<asp:Button runat="server" ID="btnYes" Text="Yes" OnClick="btnYes_Click" CssClass="btn btn-primary" />

Code-Behind:

protected void btnYes_Click(object sender, EventArgs e)
{
    if (start date is supplied && parse TextBox, out from)
    {//pseudo code
        if(end date is supplied && parse TextBox, out to)
        {//pseudo code
            using (MyEntities dc = new MyEntities())
            {
                SqlParameter paraType = new SqlParameter("@type", ddl.SelectedValue);
                SqlParameter paraSDate = new SqlParameter("@start", from);
                SqlParameter paraEDate = new SqlParameter("@end", to);
                dc.Database.ExecuteSqlCommand("sp_MyStoredProcedure @type, @start, @end", paraType, paraStart, paraEnd;
            }
        }
    }
}
toadfromgrove
  • 143
  • 2
  • 14
  • Please share some code. – bruno.almeida Nov 22 '17 at 14:37
  • Don't mess with the GUI from your controller, first of all. Instead, why not just set a bool to true while it's running, and if the button is clicked while the bool is true, cancel execution? – Brandon Miller Nov 22 '17 at 14:38
  • https://stackoverflow.com/questions/8757963/avoiding-duplicate-form-submission-in-asp-net-mvc-by-clicking-submit-twice – Steve Greene Nov 22 '17 at 15:04
  • @BrandonMiller with the bool do you mean like a HiddenField control? Because setting a bool in the method just gets reinitialized with every post. – toadfromgrove Nov 22 '17 at 16:36
  • @toadfromgrove create a public bool at the top of your class. At the top of your method if(bool == true) return;.. After checking if the bool is true and deciding it isn't, set it to true. Run your method. After method execution, set it back to false. – Brandon Miller Nov 22 '17 at 16:38
  • You need to show some code. Is it a submit button inside a form? I would usually do the disable, make the controller call via AJAX and then handle the success and failure scenarios. – Steve Greene Nov 22 '17 at 17:01
  • @BrandonMiller I tried using a HiddenField and that didn't work. The code checks the hiddenfield value for false then immediately sets to true and executes the other lines up to the execution of the sql command. at the end of that IF block it sets the hiddenfield back to false. the problem is I click the button twice and the second time through also says the hiddenfield is already false so it goes through the if block as if the sql wasn't executing. – toadfromgrove Nov 22 '17 at 19:25
  • Why are you using a hidden field? You simply put the bool as a global variable in your codebehind. If the bool is true, return. If not, set to true, run your method, and then set to false again after execution. – Brandon Miller Nov 22 '17 at 23:05
  • Stop trying to make your client take care of logic that your server can easily take care of. If the logic you're trying to prevent executing resides in the codebehind then you need to prevent it from the codebehind. This will save you or anyone else a hell of a headache later on. – Brandon Miller Nov 22 '17 at 23:07

2 Answers2

0

You can either use a nonce in the form/url and check on the server-side to avoid double-execution, or you can use JavaScript to block the button/form/page; e.g. with JQuery:

$('#myForm').submit(function(){
    $('#myForm button').attr('disabled', 'disabled');
});
Kanadaj
  • 962
  • 9
  • 25
0

Adding this line in Page_Load worked as I wanted.

btnYes.Attributes.Add("onclick", " this.disabled = true; " + ClientScript.GetPostBackEventReference(btnYes, null) + ";");

toadfromgrove
  • 143
  • 2
  • 14