2010-09-09 118 views
5

我正在使用一个名为'btn_Save'的按钮的用户控件。我需要点击用户控件'btn_Save'按钮时触发一封电子邮件。但我已经从页面后面的aspx代码执行此操作。那么,如何在使用C#的页面后面的代码中执行此操作。用户控件按钮单击事件

+0

提交页面,同时点击th按钮,并写下我们的代码if(IspostBack) – Hukam 2010-09-09 08:56:43

回答

8

我想你是问如何从用户控件的按钮单击事件作出回应父网页表单(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 
} 
0

在你btn_Save click事件

MailMessage mail = new MailMessage(); 
    mail.To = "[email protected]"; 
    mail.From = "[email protected]"; 
    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;