1

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
}
Chris
  • 470
  • 1
  • 10
  • 19

3 Answers3

2

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");
}
Shadow The GPT Wizard
  • 66,030
  • 26
  • 140
  • 208
Deepak Saralaya
  • 457
  • 4
  • 12
0

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

Martin Milan
  • 6,346
  • 2
  • 32
  • 44
0

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

Community
  • 1
  • 1
Ronald
  • 1,990
  • 6
  • 24
  • 39