我正在使用一个用户控件,其中有一个名为“btn_Save”的按钮。我需要在单击用户控件“btn_Save”按钮时发送电子邮件。但我必须从我的aspx代码后面的页面中执行此操作。那么,我如何使用C#在代码后面的页面中执行此操作。
我正在使用一个用户控件,其中有一个名为“btn_Save”的按钮。我需要在单击用户控件“btn_Save”按钮时发送电子邮件。但我必须从我的aspx代码后面的页面中执行此操作。那么,我如何使用C#在代码后面的页面中执行此操作。
我想您是在询问如何从父级Web表单(aspx页面)响应用户控件的按钮单击事件,对吗? 有几种方法可以实现...
最直接的方法是在父级Web表单的代码后台注册事件处理程序。类似于以下内容:
//web form default.aspx or whatever
protected override void OnInit(EventArgs e)
{
//find the button control within the user control
Button button = (Button)ucMyControl.FindControl("Button1");
//wire up event handler
button.Click += new EventHandler(button_Click);
base.OnInit(e);
}
void button_Click(object sender, EventArgs e)
{
//send email here
}
MailMessage mail = new MailMessage();
mail.To = "boo@far.com";
mail.From = "foo@bar.com";
mail.Subject = "Subject";
mail.Body = "This is the e-mail body";
SmtpMail.SmtpServer = "192.168.1.1"; // your smtp server address
SmtpMail.Send(mail);
请确保您正在使用 System.Web.Mail:
using System.Web.Mail;