views:

58

answers:

2

I have a user control with a button named upload in it. The button click event looks like this:

 protected void btnUpload_Click(object sender, EventArgs e)
{
  // Upload the files to the server
}

On the page where the user control is present, after the user clicks on the upload button I want to perform some operation right after the button click event code is executed in the user control. How do I tap into the click event after it has completed its work?

+3  A: 

You have to create an event in your user control, something like:

public event EventHandler ButtonClicked;

and then in your method fire the event...

protected void btnUpload_Click(object sender, EventArgs e)
{
   // Upload the files to the server

   if(ButtonClicked!=null)
      ButtonClicked(this,e);
}

Then you will be able to attach to the ButtonClicked event of your user control.

jmservera
And then make sure that the Page that contains the usercontrol is listening to the event.... this.myUploadControl.ButtonClicked += new EventHandler(MyUploadControl_ButtonClicked);Where MyUploadControl_ButtonClicked is declared in the Page class as the event handler.
Chris Dwyer
A: 

Create a Public Property in UserControl's CodeBehind:

    public Button btn
    {
        get { return this.Button1; }
    }

Then on page_Load you can use it like:

    WebUserControl11.btn.Click += (s, ea) => { Response.Write("Page Write"); };
Shoaib Shaikh