C# – fire onClick event on ascx from aspx page c#


I have save button on a dynamic usercontrol that I load onto aspx page, but I want to move the button onto .aspx page instead. How can I fire the onclick event from aspx to ascx.

Any help would be great.


Code Example:


   protected void BT_Save_Click(object sender, EventArgs e)
{//Save details currently on ascx page }


 protected void BT_aspx_Click(object sender, EventArgs e)
//when this button is clicked I need it to fire BT_Save_Click on ascx page to save the data

Best Solution

In user control

<asp:Button ID="Button1" runat="server" Text="Button"  OnClick="Button1_Click"/>

In User control .cs page

public event EventHandler ButtonClickDemo;
protected void Button1_Click(object sender, EventArgs e)
    ButtonClickDemo(sender, e);

In Page aspx page

<uc1:WebUserControl runat="server" id="WebUserControl" />

In Page.cs

protected void Page_Load(object sender, EventArgs e)
    WebUserControl.ButtonClickDemo += new EventHandler(Demo1_ButtonClickDemo);

protected void Demo1_ButtonClickDemo(object sender, EventArgs e)
    Response.Write("It's working");
Related Question