0

New here and relatively new to development and very new to ASP.

I have created a WCF service with a method that ultimately inserts a record into a SQL table and calls an email send class.

On my ASP web form that consumes the service, I have a button that invokes one of the methods.

What I dont understand and wish to fix, the method all in takes around 10 seconds to complete (I need to get this down but thats for further down the line). I can keep clicking the button before the first click has had a chance to complete and in turn send the email. With each click, it queues up and before you know it you have 10 identical emails.

I want to disable the button until the method has returned.

I have spent all day trying to resolve this and have looked and multiple forums, coming up with nothing. So any help is appreciated. - I cant work out what I'm missing.

Service Method;

public class A : IA
    {
        public int Set_A(string a, int t, string d, string c, int f)
        {
            using (var dbAbs = new Entities())
            {
                if (a != "" && d != "" && c != "")
                {
            // Do stuff - add to entity etc. 
                        if (tl != null)
                        {
                            try
                            {
                                SendMail(tl.TL_E, a, t, d, c, f);
                            }
                            catch (Exception ex)
                            {
                                throw new Exception(ex.Message);
                            }
                            Retval = dbAbs.SaveChanges();
                        }
                        else
                        {
                            retval = -99;
                        }
                        return retval;                   
                }
                else return -1;
            }
        }

Code behind the button click on the web form;

protected void AddA_Click(object sender, EventArgs e)
{
    AddA.Enabled = false;
    AddA.Visible = false;
    var absvc = new AService.AClient();

// setting up variables

    int ret = absvc.Set_A(a, ab, d, c, f);

//error handling

    AddA.Enabled = true;
    AddA.Visible = true;
}

As you can see, I've tried to use .Enabled and .Visible but they never seem to work.

Any help is appreciate, I've ive not provided enough info just shout at me!

Cheers Liam

Godber
  • 1
  • possible duplicate of: https://stackoverflow.com/questions/5189593/disable-asp-net-button-after-click-to-prevent-double-clicking – Ryan Wilson Mar 07 '18 at 16:52
  • You need to handle this, at least initially, on the client side. – TZHX Mar 07 '18 at 16:52
  • @TZHX correct, for usability, but as you hint at, client side isn't sufficient. SQL Server integrity should be leveraged, possibly through entity framework. It depends on what the action means though, it might not be critical. – Aluan Haddad Mar 07 '18 at 16:54

1 Answers1

0

Ryan Wilson was correct - It is a duplicate question.

Thanks to kmonty on other post for solving this for me.

For reference, the below worked for me.

Here is a solution that works for the asp.net button object. On the front end, add these attributes to your asp:Button definition:

<asp:Button ... OnClientClick="this.disabled=true;" UseSubmitBehavior="false" />

In the back end, in the click event handler method call, add this code to the end (preferably in a finally block)

myButton.Enabled = true;
Godber
  • 1
  • You don't need to re-enable it in code behind since the disabling is done by javascript and will be lost on PostBack. – VDWWD Mar 07 '18 at 17:39