Experienced Solution Architect, Lead developer and Author. I have 9 years of software development experience working on Microsoft technologies. I have extensive experience working with custom developed Windows and Web based .Net projects with primarily C# as a language. My major interests involve in designing frameworks and architecture for software projects. I am a learning passionate and always wants to explore new and upcoming Microsoft technologies. I also run a Meetup group named as “Microsoft Technology Practices User Group”. The vision of this group is to share and explore latest technologies on Microsoft platform. We used to provide trainings, demo sessions, technology overview in group meetings and we also welcome professionals to present and give an insight on any technology in which they are expert and present. Moreover, our group has been sponsored by Plural Sight which helps us to share and learn from the webcasts published there. If anyone is interested to join my Meetup group please visit the following site http://meetup.com/MSTPUG or send me an email at ovaismehboob@yahoo.com to become a group member. Ovais is a DZone MVB and is not an employee of DZone and has posted 8 posts at DZone. You can read more from them at their website. View Full User Profile

Handling User Control Events in ASP.Net

10.03.2012
| 3328 views |
  • submit to reddit

Although most developers know how to handle events of User Controls, this will help for those developers who have never come across this before and wants to learn with a simple example the details about how to handle custom user control events on aspx pages.

What are User Controls? 

User controls are just like standard ASP.Net webform with few limitations. It contains a designer view where the user can drag and drop existing controls of ASP.net or third party to structure that user control. Inorder to learn more about User control you can follow this link http://msdn.microsoft.com/en-us/library/y6wb1a0e(v=vs.100).aspx

How to Handling custom developed User Control Event

Let suppose we have a user control containing a textbox and a button. And we have an aspx page where we are using that control. Now inorder to handle the button click event of the User control's button click event on aspx page we will go through the following steps.

Following is a code snippet of User Control named "ctrlCustomUserControl.ascx"

<%@ Control Language="C#" AutoEventWireup="true" CodeBehind="ctrlMyUserControl.ascx.cs" Inherits="WebApplication7.ctrlMyUserControl" %>
<asp:TextBox ID="txtBox" runat="server"></asp:TextBox>
<asp:Button ID="btnClickMe" runat="server" Text="Click Me!" 
    onclick="btnClickMe_Click" />

You can see there are two controls placed, one is textbox and the other is button.

Code behind file looks like as follows

 public partial class ctrlMyUserControl : System.Web.UI.UserControl
    {

        public delegate void ClickHandler(object sender, EventArgs e);
        public event ClickHandler Click;

        protected void btnClickMe_Click(object sender, EventArgs e)
        {
            Click(sender, e);
        }

        public String Text
        {
            set { this.txtBox.Text = value; }
        }
    }

 Above code shows that there is a delegate named ClickHandler and I have declare its event named Click. And I have also registered the button click event and called my custom Click event there, which will be registered by the ASPX page will be invoked from this event.

Now, lets look into the "Default.aspx" page where I am using this control.

<%@ Page Title="Home Page" Language="C#" AutoEventWireup="true"
    CodeBehind="Default.aspx.cs" Inherits="WebApplication7._Default" %>
<%@ Register src="~/ctrlMyUserControl.ascx" TagName="MyControl" TagPrefix="uc"%>
<form runat="server">
   <uc:MyControl id="myctrl" runat="server" OnClick="myctrl_click"></uc:MyControl>
</form>

Second statement shows the registration of my custom user control on aspx page, tagprefix and tagname values are used to define control tag.

I have used uc as the tagprefix and MyControl as tagname. You can see how I have specified the markup of custom user control. One important thing, you can see that I have specified OnClick event. Actually if you recall the control code behind file there I have an event named "Click" and inorder to register it on any aspx page we have to add a prefix "On" i.e. "OnClick". This is how we can register custom events of user the control on aspx page.

Code behind of Default.aspx is as follows 

  public partial class _Default : System.Web.UI.Page
    {
        protected void myctrl_click(object sender, EventArgs e)
        {
            myctrl.Text = "Hello World!";
        }
    }

When user clicks on the button, it will populate "Hello World!" text on the text box. 

Happy Coding! 

 

 

Published at DZone with permission of Ovais Mehboob Ahmed Khan, author and DZone MVB.

(Note: Opinions expressed in this article and its replies are the opinions of their respective authors and not those of DZone, Inc.)