0

我有一个发送电子邮件的复合自定义控件。邮件在按钮单击时发送。但是,邮件是在我单击按钮两次后发送的。有谁知道为什么会这样,我该如何解决?这是代码:

namespace EmailControl
{
    [DefaultProperty("Text")]
    [ToolboxData("<{0}:ServerControl1 runat=server></{0}:ServerControl1>")]
    public class ServerControl1 : WebControl,INamingContainer
    {
        public string Text
        {
            get { return ViewState["Text"] as string ?? "[" + this.ID + "]"; }
            set { ViewState["Text"] = value; }
        }

        public string Host
        {
            get { return ViewState["host"] as string ?? "[" + this.ID + "]"; }
            set { ViewState["host"] = value; }
        }

        public string From
        {
            get { return ViewState["from"] as string ?? "[" + this.ID + "]"; }
            set { ViewState["from"] = value; }
        }

        public int Port
        {
            get { return (int) ViewState["port"]; }
            set { ViewState["port"] = value; }
        }

        public string UserName
        {
            get { return ViewState["user"] as string ?? "[" + this.ID + "]"; }
            set { ViewState["user"] = value; }
        }

        public string Password
        {
            get { return ViewState["pass"] as string ?? "[" + this.ID + "]"; }
            set { ViewState["pass"] = value; }
        }

        TextBox txtEmail;
        TextBox txtBody;
        TextBox txtSubject;
        Button btnSend;

        protected override void CreateChildControls()
        {
            txtEmail = new TextBox();
            txtEmail.ID = "txtEmail";
            txtEmail.AutoPostBack = true;
            LiteralControl l1 = new LiteralControl("<br/>");
            txtSubject = new TextBox();
            txtSubject.ID = "txtSubject";
            txtEmail.AutoPostBack = true;
            this.Controls.Add(txtEmail);
            this.Controls.Add(txtSubject);
            LiteralControl l2= new LiteralControl("<br/>");
            txtBody = new TextBox();
            txtBody.ID = "txtBody";
            txtBody.AutoPostBack = true;
            txtBody.TextMode = TextBoxMode.MultiLine;
            txtBody.Width = 200;
            txtBody.Height = 200;
            this.Controls.Add(txtBody);
            btnSend = new Button();
            btnSend.ID = "btnSend";
            btnSend.Width = 100;
            btnSend.Text = "Send";
            btnSend.Click += new EventHandler(OnButtonClick);
            this.Controls.Add(btnSend);
        }

        protected void OnButtonClick(object sender,EventArgs e)
        {
            TextBox txtEmail = (TextBox)this.FindControl("txtEmail");
            MailMessage msg = new MailMessage(From,txtEmail.Text);
            TextBox txtSubject = (TextBox)this.FindControl("txtSubject");
            TextBox txtBody = (TextBox)this.FindControl("txtBody");
            msg.Subject = txtSubject.Text;
            msg.Body = txtBody.Text;

            SmtpClient smtp = new SmtpClient();
            smtp.Host = Host;
            smtp.Port = Convert.ToInt32(Port);
            smtp.EnableSsl = true;
            smtp.Credentials = new System.Net.NetworkCredential(UserName,
                Password);
            smtp.Send(msg);
        }
    }
}

aspx页面中的代码是:

protected void Page_Load(object sender, EventArgs e)
{
    ServerControl11.Host = "smtp.gmail.com";
    ServerControl11.Port =25;
    ServerControl11.UserName = "*******************@gmail.com";
    ServerControl11.Password = "************";
    ServerControl11.From = "******************@gmail.com";
}
4

1 回答 1

1

您是说每次单击按钮时都会发送电子邮件?如果是这样,您只需要防止第二次回发。看这里:

如何防止重复回发混淆我的业务层

于 2012-08-05T12:58:12.187 回答