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

Stats

150.1K views
3K downloads
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.

Introduction

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.

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

Now add a button to this user control.

ASP.NET
<% @ 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.

C#
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.

C#
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

License

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

Share

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 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
Talking Dotnet22-Dec-14 0:18
MemberTalking Dotnet22-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
professionalquangphuoc10-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 Cao2-May-12 4:11
MemberDan Cao2-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
professionalKanwal Shehzad5-Oct-09 20:59 
GeneralRe: What's alternate in VB.Net Pin
Talking Dotnet7-Oct-09 18:34
MemberTalking Dotnet7-Oct-09 18:34 
GeneralGood stuff Pin
Dilip Baboo21-Aug-09 15:07
MemberDilip Baboo21-Aug-09 15:07 
GeneralRe: Good stuff Pin
Talking Dotnet7-Oct-09 18:36
MemberTalking Dotnet7-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
Talking Dotnet13-May-09 2:43
MemberTalking Dotnet13-May-09 2:43 
GeneralMy vote of 1 Pin
CARPETBURNER5-Mar-09 0:35
MemberCARPETBURNER5-Mar-09 0:35 
GeneralRe: My vote of 1 Pin
Talking Dotnet8-Mar-09 19:04
MemberTalking Dotnet8-Mar-09 19:04 
GeneralNothing like having an article that states the obivious! Pin
CARPETBURNER5-Mar-09 0:35
MemberCARPETBURNER5-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.