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#.
A:
Your first step should be to search the web for code that demonstrates how to email someone using c#
For example, I searched for "sending email with c#" using Google, and I got this as my first result:
Which looks like it explains what you need to know.
Matt Ellen
2010-09-09 08:57:40
A:
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;
Brissles
2010-09-09 09:02:49
A:
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
}
Kurt Schindler
2010-09-09 12:03:31