C# – User control button click event

asp.netcuser-controls

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#.

Best Answer

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
}