Click here to Skip to main content
Licence CPOL
First Posted 4 Mar 2009
Views 32,756
Downloads 491
Bookmarked 10 times

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

By Virendra Dugar | 4 Mar 2009
Solution to capture button’s click placed in user control inside the page that holds the user control.
1 vote, 11.1%
1
2 votes, 22.2%
2

3
1 vote, 11.1%
4
5 votes, 55.6%
5
3.80/5 - 9 votes
μ 3.80, σa 3.07 [?]

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)

About the Author

Virendra Dugar

Software Developer (Senior)

India India

Member
Virendra Dugar is experienced Senior Software Developer with 4+ years of hands-on experience working with Microsoft .NET technology (ASP.NET, C#, VB.NET,SQL Server). He is always keen to learn new technology. He holds a Master's Degree in Computer Application & Information technology from Gujarat University, India. In free time, he loves to listen music, read books, play games and do blogging etc.
 
Check out his Blog :- http://jquerybyexample.blogspot.com

Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board. (secure sign-in)
 
Search this forum  
 FAQ
    Noise  Layout  Per page   
  Refresh
Questiongood article Pinmemberali66882hrs 10mins ago 
GeneralGood Job PinmemberSreejith Gopinathan19:05 11 Feb '11  
GeneralA good informative Tip, Not an article PinmemberHiren Solanki20:25 14 Dec '10  
GeneralMy vote of 2 PinmemberLokesh Lal22:07 11 May '10  
GeneralWhat's alternate in VB.Net PinmemberKanwal Shehzad20:59 5 Oct '09  
GeneralRe: What's alternate in VB.Net PinmemberVirendra Dugar18:34 7 Oct '09  
GeneralGood stuff PinmemberDilip Baboo15:07 21 Aug '09  
GeneralRe: Good stuff PinmemberVirendra Dugar18:36 7 Oct '09  
QuestionThis post helped me. Pinmembervoleona22:16 3 Aug '09  
GeneralBasic - but good post. PinmemberLance W. Larsen12:11 11 May '09  
GeneralRe: Basic - but good post. PinmemberVirendra Dugar2:43 13 May '09  
GeneralMy vote of 1 PinmemberGriffinPeter0:35 5 Mar '09  
GeneralRe: My vote of 1 PinmemberVirendra Dugar19:04 8 Mar '09  
GeneralNothing like having an article that states the obivious! PinmemberGriffinPeter0:35 5 Mar '09  
GeneralMy vote of 2 PinmemberGuillaume Leparmentier22:38 4 Mar '09  
QuestionWhat was that? PinmemberALGR16:12 4 Mar '09  

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.

Permalink | Advertise | Privacy | Mobile
Web01 | 2.5.120210.1 | Last Updated 4 Mar 2009
Article Copyright 2009 by Virendra Dugar
Everything else Copyright © CodeProject, 1999-2012
Terms of Use
Layout: fixed | fluid