[英]Custom control OnButtonClick event handler overriding C#
I have a composite custom control that sends an e-mail. 我有一个发送电子邮件的复合自定义控件。 The mail is send on button click. 单击按钮发送邮件。 However, the mail is sent after I have clicked on a button twice. 但是,单击两次按钮后,邮件将发送出去。 Does anybody know why this is happening and how can I solve it? 有人知道为什么会这样吗,我该如何解决? Here is the code: 这是代码:
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);
}
}
}
The code in the aspx page is: 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";
}
You're saying that the email is sent every time you click the button? 您是说每次单击按钮都会发送电子邮件吗? If so, you just need to prevent the second postback. 如果是这样,您只需要防止第二次回发。 See here: 看这里:
How to prevent repeated postbacks from confusing my business layer 如何防止重复的回发混淆我的业务层
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.