Capture button’s click event of user control in the page.
Posted by Virendra Dugar on February 22, 2009
Yesterday one of my colleagues was facing a problem with button placed in user control. He wanted to capture button’s click placed in user control inside the page that holds the user control.
I made a sample application for him. I thought I should share with you also.
Download Sample Code.
This is my user control (.ascx) file. As you can see I have placed a button and onclick event is also defined. (See image.)
If you want to extend the button click placed in the user control to the page, then declare an Event handler and on the click of the button just make a call to the declared event handler. This will work as a property for any web page. See below image.
Place the control in your .aspx page. In the Page_Load event of your page, assign a new event handler to the User control’s event handler property. And, now use the newly declared event handler.
So, now when button (which is placed in user control) is clicked, event handler declared in the page will be used to handle the click event.