简体   繁体   English

C#asp.net cclick事件触发两次

[英]C# asp.net cclick event fires twice

I have a click event in C# code behind file that sends an e-mail, everything works fine except that it sends the email twice. 我在发送电子邮件的文件后面的C#代码中有一个click事件,除发送两次电子邮件外,其他一切正常。

I tried the recommended solution and set AutoEventWireup="false" , this does nothing it still sends the email twice. 我尝试了推荐的解决方案,并设置了AutoEventWireup="false" ,但它仍然没有执行两次发送电子邮件的操作。

Any ideas? 有任何想法吗?

Thanks in advance 提前致谢

Set <% @Page AutoEventWireup="true" %> to false . <% @Page AutoEventWireup="true" %>false

protected void cmdNotify_Click(object sender, EventArgs e)
{
    newmail = new mymail();
    newmail.SendMail();
}

Email sends twice. 电子邮件发送两次。

I am not sure why people suggest you turn AutoEventWireup off. 我不确定为什么有人建议您关闭AutoEventWireup Don't do that unless you want to manually wire up events. 除非要手动连接事件,否则不要这样做。 For example, you need to write the following line to have any code in the Page_Load event handler executed when the page is requested: 例如,您需要编写以下行,以便在请求页面时在Page_Load事件处理程序中执行任何代码:

Load += new EventHandler(Page_Load);

So, let AutoEventWireup to its default value ( true ) and make sure no such code exists in your code behind. 因此,让AutoEventWireup为其默认值( true ),并确保后面的代码中没有此类代码。

Another important thing to understand is that this has not to do with sending the email per se but with the life cycle of your page. 要理解的另一件重要事情是,这与发送电子邮件本身无关,而与页面的生命周期有关。 You may have code in Page_Load that triggers the mail sending code or even JavaScript code that raises the event. 您可能在Page_Load中具有触发邮件发送代码的代码,甚至触发该事件的JavaScript代码。 Make sure this is not the case by cleaning up your code. 通过清理代码来确保不是这种情况。 For example, you may want to start over with the following sample page which demonstrates that normally button click events are raised only once per request and gradually add your code to it: 例如,您可能想从下面的示例页面开始,该页面演示通常每个请求仅引发一次按钮单击事件,并逐渐向其中添加代码:

ASPX ASPX

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm1.aspx.cs" Inherits="WebApplication1.WebForm1" %>

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
        <div>
            <asp:Button ID="Button1" runat="server" Text="Button" OnClick="Button1_Click" />
            <br />
            <br />
            <asp:Label ID="Label1" runat="server"></asp:Label>
        </div>
    </form>
</body>
</html>

Code behind 后面的代码

namespace WebApplication1
{
    public partial class WebForm1 : System.Web.UI.Page
    {
        private static int times = 0;

        protected void Page_Load(object sender, EventArgs e)
        {
        }

        protected void Button1_Click(object sender, EventArgs e)
        {
            times++;
            Label1.Text = times.ToString();
        }
    }
}

Hope it helps. 希望能帮助到你。 I am using Visual Studio 2017 and the target framework is .NET Framework 4.6.1. 我正在使用Visual Studio 2017,目标框架是.NET Framework 4.6.1。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM