【问题标题】:ASP.NET Webform - how to stop the clicking of a button multiple timesASP.NET Webform - 如何停止多次单击按钮
【发布时间】:2018-08-15 20:51:21
【问题描述】:

这里是新的,对开发来说相对较新,对 ASP 来说也很新。

我创建了一个 WCF 服务,其方法最终将记录插入 SQL 表并调用电子邮件发送类。

在我使用该服务的 ASP Web 表单上,我有一个调用其中一种方法的按钮。

我不明白并希望解决的问题,全部方法大约需要 10 秒才能完成(我需要把它弄下来,但那是为了进一步的下线)。在第一次点击有机会完成之前,我可以继续点击按钮,然后发送电子邮件。每次点击,它都会排队,在你知道它之前,你有 10 封相同的电子邮件。

我想在方法返回之前禁用按钮。

我花了一整天的时间试图解决这个问题,并查看了多个论坛,但一无所获。所以任何帮助表示赞赏。 - 我无法弄清楚我错过了什么。

服务方法;

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;
            }
        }

网页表单点击按钮背后的代码;

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;
}

如您所见,我尝试使用 .Enabled 和 .Visible 但它们似乎从来没有工作过。

感谢您的帮助,我没有提供足够的信息,只是对我大喊大叫!

干杯 利亚姆

【问题讨论】:

  • 您需要在客户端处理这个问题,至少最初是这样。
  • @TZHX 正确,为了可用性,但正如您所暗示的,客户端是不够的。应该利用 SQL Server 完整性,可能通过实体框架。这取决于操作的含义,但可能并不重要。

标签: c# asp.net webforms


【解决方案1】:

Ryan Wilson 是正确的 - 这是一个重复的问题。

感谢其他帖子上的 kmonty 为我解决了这个问题。

作为参考,以下内容对我有用。

这是一个适用于 asp.net 按钮对象的解决方案。在 前端,将这些属性添加到您的 asp:Button 定义中:

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

在后端,在点击事件处理方法调用中,添加这段代码 到最后(最好在 finally 块中)

myButton.Enabled = true;

【讨论】:

  • 您不需要在后面的代码中重新启用它,因为禁用是由 javascript 完成的,并且会在 PostBack 上丢失。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-11-13
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-09-30
相关资源
最近更新 更多