DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports Events Over 2 million developers have joined DZone. Join Today! Thanks for visiting DZone today,
Edit Profile Manage Email Subscriptions Moderation Admin Console How to Post to DZone Article Submission Guidelines
View Profile
Sign Out
Refcards
Trend Reports
Events
Zones
Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks

Handling User Control Events in ASP.Net

Ovais Mehboob Ahmed Khan user avatar by
Ovais Mehboob Ahmed Khan
·
Oct. 03, 12 · Interview
Like (0)
Save
Tweet
Share
15.02K Views

Join the DZone community and get the full member experience.

Join For Free

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! 

 

 

Event

Opinions expressed by DZone contributors are their own.

Popular on DZone

  • What Was the Question Again, ChatGPT?
  • Microservices Discovery With Eureka
  • API Design Patterns Review
  • What Is a Kubernetes CI/CD Pipeline?

Comments

Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 600 Park Offices Drive
  • Suite 300
  • Durham, NC 27709
  • support@dzone.com
  • +1 (919) 678-0300

Let's be friends: