Click here to Skip to main content
Click here to Skip to main content
Go to top

Capture Button’s Click Event of User Control in the Page

, 4 Mar 2009
Rate this:
Please Sign up or sign in to vote.
Solution to capture button’s click placed in user control inside the page that holds the user control.


Almost every application nowadays makes use of UserControl. This article helps you to understand how one can capture the button's (placed in User Control) click event inside the page which holds the user control.

Using the Code

To start with, let's first create a new website and add a user control to it and name it Demo.ascx. This is how your user control looks like initially.

<% @ Control Language="C#" AutoEventWireup="true" 
	CodeFile="Demo.ascx.cs" Inherits="Demo" %> 

Now add a button to this user control.

<% @ Control Language="C#" AutoEventWireup="true" 
		CodeFile="Demo.ascx.cs" Inherits="Demo" %>

<asp:Button ID="btnClick" runat="server" Text="Click Me!!!" onclick="btnClick_Click" />

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.

public event EventHandler ButtonClickDemo;

protected void Page_Load(object sender, EventArgs e)
protected void btnClick_Click(object sender, EventArgs e)
   ButtonClickDemo(sender, e);

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.

public partial class _Default : System.Web.UI.Page 
    protected void Page_Load(object sender, EventArgs e)
        Demo1.ButtonClickDemo += new EventHandler(Demo1_ButtonClickDemo);

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

Now when the button (which is placed in user control) is clicked, event handler declared in the page will be used to handle the click event.


  • 4th March, 2009: Initial post


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

jQuery By Example
Technical Lead
India India
I am an experienced Software Developer with 6+ years of hands-on experience working with Microsoft .NET technology (ASP.NET, C#, VB.NET,SQL Server).
Visit my blog and you can also follow me on Facebook
Follow on   Twitter   Google+

Comments and Discussions

GeneralMy vote of 5 PinmemberAbhijoy_D5-Apr-13 21:39 
QuestionGood job Pinmemberquangphuoc10-Dec-12 17:44 
GeneralMy vote of 5 PinmemberMember 313707812-Sep-12 20:04 
GeneralMy vote of 4 Pinmemberblpandya25-Jul-12 8:14 
GeneralIt's useful PinmemberDan, Cao Huy2-May-12 3:11 
QuestionUseful PinmemberTimo Hermans25-Apr-12 23:59 
Questiongood article Pinmemberali668812-Feb-12 1:10 
GeneralGood Job PinmemberSreejith Gopinathan11-Feb-11 18:05 
GeneralA good informative Tip, Not an article PinmemberHiren Solanki14-Dec-10 19:25 
GeneralMy vote of 2 PinmemberLokesh Lal11-May-10 21:07 
GeneralWhat's alternate in VB.Net PinmemberKanwal Shehzad5-Oct-09 19:59 
GeneralRe: What's alternate in VB.Net PinmemberVirendra Dugar7-Oct-09 17:34 
GeneralGood stuff PinmemberDilip Baboo21-Aug-09 14:07 
GeneralRe: Good stuff PinmemberVirendra Dugar7-Oct-09 17:36 
QuestionThis post helped me. Pinmembervoleona3-Aug-09 21:16 
GeneralBasic - but good post. PinmemberLance W. Larsen11-May-09 11:11 
GeneralRe: Basic - but good post. PinmemberVirendra Dugar13-May-09 1:43 
GeneralMy vote of 1 PinmemberGriffinPeter4-Mar-09 23:35 
GeneralRe: My vote of 1 PinmemberVirendra Dugar8-Mar-09 18:04 
GeneralNothing like having an article that states the obivious! PinmemberGriffinPeter4-Mar-09 23:35 
GeneralMy vote of 2 PinmemberGuillaume Leparmentier4-Mar-09 21:38 
QuestionWhat was that? PinmemberALGR4-Mar-09 15:12 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Mobile
Web01 | 2.8.140916.1 | Last Updated 4 Mar 2009
Article Copyright 2009 by jQuery By Example
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid