Click here to Skip to main content
11,637,176 members (61,828 online)
Click here to Skip to main content

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

, 4 Mar 2009 CPOL 91.2K 2.3K 13
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.

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.

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

History

  • 4th March, 2009: Initial post

License

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

Share

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

You may also be interested in...

Comments and Discussions

 
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
jQuery By Example21-Dec-14 23:18
memberjQuery By Example21-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
memberquangphuoc10-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, Cao Huy2-May-12 3:11
memberDan, Cao Huy2-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
memberKanwal Shehzad5-Oct-09 19:59 
GeneralRe: What's alternate in VB.Net Pin
Virendra Dugar7-Oct-09 17:34
memberVirendra Dugar7-Oct-09 17:34 
GeneralGood stuff Pin
Dilip Baboo21-Aug-09 14:07
memberDilip Baboo21-Aug-09 14:07 
GeneralRe: Good stuff Pin
Virendra Dugar7-Oct-09 17:36
memberVirendra Dugar7-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
Virendra Dugar13-May-09 1:43
memberVirendra Dugar13-May-09 1:43 
GeneralMy vote of 1 Pin
GriffinPeter4-Mar-09 23:35
memberGriffinPeter4-Mar-09 23:35 
GeneralRe: My vote of 1 Pin
Virendra Dugar8-Mar-09 18:04
memberVirendra Dugar8-Mar-09 18:04 
GeneralNothing like having an article that states the obivious! Pin
GriffinPeter4-Mar-09 23:35
memberGriffinPeter4-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    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 | Terms of Use | Mobile
Web02 | 2.8.150728.1 | Last Updated 4 Mar 2009
Article Copyright 2009 by jQuery By Example
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid