Reputation: 15617
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 behind page using C#.
Upvotes: 5
Views: 13830
Reputation: 21357
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
}
Upvotes: 8
Reputation: 3891
In your btn_Save click event
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);
Make sure you are Using System.Web.Mail:
using System.Web.Mail;
Upvotes: 0