Click here to Skip to main content
Licence CPOL
First Posted 4 Mar 2009
Views 38,096
Downloads 688
Bookmarked 11 times

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

By | 4 Mar 2009 | Article
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)

About the Author

Virendra Dugar

Software Developer (Senior)

India India

Member

Virendra Dugar is experienced Senior Software Developer with 5+ 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
GeneralIt's useful PinmemberDan, Cao Huy3:11 2 May '12  
QuestionUseful PinmemberTimo Hermans23:59 25 Apr '12  
Questiongood article Pinmemberali66881:10 12 Feb '12  
GeneralGood Job PinmemberSreejith Gopinathan18:05 11 Feb '11  
GeneralA good informative Tip, Not an article PinmemberHiren Solanki19:25 14 Dec '10  
GeneralMy vote of 2 PinmemberLokesh Lal21:07 11 May '10  
GeneralWhat's alternate in VB.Net PinmemberKanwal Shehzad19:59 5 Oct '09  
GeneralRe: What's alternate in VB.Net PinmemberVirendra Dugar17:34 7 Oct '09  
GeneralGood stuff PinmemberDilip Baboo14:07 21 Aug '09  
GeneralRe: Good stuff PinmemberVirendra Dugar17:36 7 Oct '09  
QuestionThis post helped me. Pinmembervoleona21:16 3 Aug '09  
GeneralBasic - but good post. PinmemberLance W. Larsen11:11 11 May '09  
GeneralRe: Basic - but good post. PinmemberVirendra Dugar1:43 13 May '09  
GeneralMy vote of 1 PinmemberGriffinPeter23:35 4 Mar '09  
GeneralRe: My vote of 1 PinmemberVirendra Dugar18:04 8 Mar '09  
GeneralNothing like having an article that states the obivious! PinmemberGriffinPeter23:35 4 Mar '09  
GeneralMy vote of 2 PinmemberGuillaume Leparmentier21:38 4 Mar '09  
QuestionWhat was that? PinmemberALGR15: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
Web03 | 2.5.120517.1 | Last Updated 4 Mar 2009
Article Copyright 2009 by Virendra Dugar
Everything else Copyright © CodeProject, 1999-2012
Terms of Use
Layout: fixed | fluid