Click here to Skip to main content
13,409,546 members (38,112 online)
Click here to Skip to main content
Add your own
alternative version


15 bookmarked
Posted 4 Mar 2009

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.

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 8+ years of hands-on experience working with Microsoft .NET technology (ASP.NET, C#, VB.NET,SQL Server).

Visit Talking Dotnet
Read Difference between Angular 1.x and Angular 2
For ASP.NET Core, read ASP.NET Core Articles
For Entity Framework Core, read Entity Framework Core Articles

You may also be interested in...


Comments and Discussions

QuestionBut what if there are 2 buttons on the user control? Pin
marc11h19-Feb-16 6:13
membermarc11h19-Feb-16 6:13 
QuestionWhat is the demo1 that you used? Pin
Deb-Samrat4-Nov-14 3:56
memberDeb-Samrat4-Nov-14 3:56 
AnswerRe: What is the demo1 that you used? Pin
jQuery By Example22-Dec-14 0:18
memberjQuery By Example22-Dec-14 0:18 
GeneralMy vote of 5 Pin
Abhijoy_D5-Apr-13 22:39
memberAbhijoy_D5-Apr-13 22:39 
QuestionGood job Pin
quangphuoc10-Dec-12 18:44
memberquangphuoc10-Dec-12 18:44 
GeneralMy vote of 5 Pin
Member 313707812-Sep-12 21:04
memberMember 313707812-Sep-12 21:04 
GeneralMy vote of 4 Pin
blpandya25-Jul-12 9:14
memberblpandya25-Jul-12 9:14 
GeneralIt's useful Pin
Dan, Cao Huy2-May-12 4:11
memberDan, Cao Huy2-May-12 4:11 
QuestionUseful Pin
Timo Hermans26-Apr-12 0:59
memberTimo Hermans26-Apr-12 0:59 
Questiongood article Pin
ali668812-Feb-12 2:10
memberali668812-Feb-12 2:10 
GeneralGood Job Pin
Sreejith Gopinathan11-Feb-11 19:05
memberSreejith Gopinathan11-Feb-11 19:05 
GeneralA good informative Tip, Not an article Pin
Hiren Solanki14-Dec-10 20:25
memberHiren Solanki14-Dec-10 20:25 
GeneralMy vote of 2 Pin
Lokesh Lal11-May-10 22:07
memberLokesh Lal11-May-10 22:07 
GeneralWhat's alternate in VB.Net Pin
Kanwal Shehzad5-Oct-09 20:59
memberKanwal Shehzad5-Oct-09 20:59 
GeneralRe: What's alternate in VB.Net Pin
Virendra Dugar7-Oct-09 18:34
memberVirendra Dugar7-Oct-09 18:34 
GeneralGood stuff Pin
Dilip Baboo21-Aug-09 15:07
memberDilip Baboo21-Aug-09 15:07 
GeneralRe: Good stuff Pin
Virendra Dugar7-Oct-09 18:36
memberVirendra Dugar7-Oct-09 18:36 
QuestionThis post helped me. Pin
voleona3-Aug-09 22:16
membervoleona3-Aug-09 22:16 
GeneralBasic - but good post. Pin
Lance W. Larsen11-May-09 12:11
memberLance W. Larsen11-May-09 12:11 
GeneralRe: Basic - but good post. Pin
Virendra Dugar13-May-09 2:43
memberVirendra Dugar13-May-09 2:43 
GeneralMy vote of 1 Pin
GriffinPeter5-Mar-09 0:35
memberGriffinPeter5-Mar-09 0:35 
GeneralRe: My vote of 1 Pin
Virendra Dugar8-Mar-09 19:04
memberVirendra Dugar8-Mar-09 19:04 
GeneralNothing like having an article that states the obivious! Pin
GriffinPeter5-Mar-09 0:35
memberGriffinPeter5-Mar-09 0:35 
GeneralMy vote of 2 Pin
Guillaume Leparmentier4-Mar-09 22:38
memberGuillaume Leparmentier4-Mar-09 22:38 
QuestionWhat was that? Pin
ALGR4-Mar-09 16:12
memberALGR4-Mar-09 16: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.

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web04 | 2.8.180221.1 | Last Updated 4 Mar 2009
Article Copyright 2009 by Talking Dotnet
Everything else Copyright © CodeProject, 1999-2018
Layout: fixed | fluid