Click here to Skip to main content
15,309,768 members
Articles / Web Development / ASP.NET
Posted 4 Mar 2009


17 bookmarked

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

Rate me:
Please Sign up or sign in to vote.
4.41/5 (18 votes)
4 Mar 2009CPOL1 min read
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.

Also, Read my series of articles on ASP.NET 5


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


About the Author

Talking Dotnet
Technical Lead
India India
I am an experienced Software Developer with 11+ years of hands-on experience working with Microsoft.NET technology (ASP.NET, ASP.NET Core, C#, SQL Server, Angular).

Visit Talking Dotnet
For ASP.NET Core, read ASP.NET Core Articles

Comments and Discussions

QuestionBut what if there are 2 buttons on the user control? Pin
marc11h19-Feb-16 5:13
Membermarc11h19-Feb-16 5:13 
QuestionWhat is the demo1 that you used? Pin
Deb-Samrat4-Nov-14 2:56
MemberDeb-Samrat4-Nov-14 2:56 
AnswerRe: What is the demo1 that you used? Pin
Talking Dotnet21-Dec-14 23:18
MemberTalking Dotnet21-Dec-14 23:18 
GeneralMy vote of 5 Pin
Abhijoy_D5-Apr-13 21:39
MemberAbhijoy_D5-Apr-13 21:39 
QuestionGood job Pin
quangphuoc10-Dec-12 17:44
professionalquangphuoc10-Dec-12 17:44 
GeneralMy vote of 5 Pin
Member 313707812-Sep-12 20:04
MemberMember 313707812-Sep-12 20:04 
GeneralMy vote of 4 Pin
blpandya25-Jul-12 8:14
Memberblpandya25-Jul-12 8:14 
GeneralIt's useful Pin
Dan Cao2-May-12 3:11
MemberDan Cao2-May-12 3:11 
QuestionUseful Pin
Timo Hermans25-Apr-12 23:59
MemberTimo Hermans25-Apr-12 23:59 
Questiongood article Pin
ali668812-Feb-12 1:10
Memberali668812-Feb-12 1:10 
GeneralGood Job Pin
Sreejith Gopinathan11-Feb-11 18:05
MemberSreejith Gopinathan11-Feb-11 18:05 
GeneralA good informative Tip, Not an article Pin
Hiren solanki14-Dec-10 19:25
MemberHiren solanki14-Dec-10 19:25 
GeneralMy vote of 2 Pin
Lokesh Lal11-May-10 21:07
MemberLokesh Lal11-May-10 21:07 
GeneralWhat's alternate in VB.Net Pin
Kanwal Shehzad5-Oct-09 19:59
professionalKanwal Shehzad5-Oct-09 19:59 
GeneralRe: What's alternate in VB.Net Pin
Talking Dotnet7-Oct-09 17:34
MemberTalking Dotnet7-Oct-09 17:34 
GeneralGood stuff Pin
Dilip Baboo21-Aug-09 14:07
MemberDilip Baboo21-Aug-09 14:07 
GeneralRe: Good stuff Pin
Talking Dotnet7-Oct-09 17:36
MemberTalking Dotnet7-Oct-09 17:36 
QuestionThis post helped me. Pin
voleona3-Aug-09 21:16
Membervoleona3-Aug-09 21:16 
GeneralBasic - but good post. Pin
Lance W. Larsen11-May-09 11:11
MemberLance W. Larsen11-May-09 11:11 
GeneralRe: Basic - but good post. Pin
Talking Dotnet13-May-09 1:43
MemberTalking Dotnet13-May-09 1:43 
GeneralMy vote of 1 Pin
CARPETBURNER4-Mar-09 23:35
MemberCARPETBURNER4-Mar-09 23:35 
GeneralRe: My vote of 1 Pin
Talking Dotnet8-Mar-09 18:04
MemberTalking Dotnet8-Mar-09 18:04 
GeneralNothing like having an article that states the obivious! Pin
CARPETBURNER4-Mar-09 23:35
MemberCARPETBURNER4-Mar-09 23:35 
GeneralMy vote of 2 Pin
Guillaume Leparmentier4-Mar-09 21:38
MemberGuillaume Leparmentier4-Mar-09 21:38 
QuestionWhat was that? Pin
ALGR4-Mar-09 15:12
MemberALGR4-Mar-09 15:12 

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

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