Chris
Chris

Reputation: 470

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.

Cheers.

Code Example:

ascx:

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

aspx:

 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
}

Upvotes: 1

Views: 10737

Answers (3)

Deepak Saralaya
Deepak Saralaya

Reputation: 457

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");
}

Upvotes: 2

Ronald
Ronald

Reputation: 2100

You'll need to create a custom event on your ascx user control and subscribe it within your aspx page.

have a look at this question

define Custom Event for WebControl in asp.net

Upvotes: 0

Martin Milan
Martin Milan

Reputation: 6390

Write a public / internal sub on the ASCX, and then call it from the onClick on the ASPX?

Upvotes: 0

Related Questions