User control button click event
I am using an user control which have a button named 'btn_Save'.I need to fire an email on clicking the user control 'btn_Save' button. But I have do this from my aspx code behind page.So, How can I do this in code开发者_Python百科 behind page using C#.
I think you are asking how to respond to the user control's button click event from the parent web form (aspx page), right? This could be done a few ways...
The most direct way would be to register an event handler on the parent web form's code behind. Something like:
//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
}
In your btn_Save click event
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);
Make sure you are Using System.Web.Mail:
using System.Web.Mail;
精彩评论