views:

69

answers:

3

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:

http://social.msdn.microsoft.com/Forums/en-US/netfxnetcom/thread/a75533eb-131b-4ff3-a3b2-b6df87c25cc8

Which looks like it explains what you need to know.

Matt Ellen
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
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