Home > DotNet > How to handle usercontrol events in aspx page?

How to handle usercontrol events in aspx page?

Raise a event from usercontrol to aspx page 

ASP.Net  applications with user controls need to commucinate with usercontrols and aspx page. We can sent parameter as properties to usercontrol from aspx page and also we can access the usercontrol controls from aspx page. This post describing how we can communicate between aspx page and usercontrols. 

Multiple usercontrols in a single aspx page communication

 Here we consider the scenario, we have two usercontrols in a single page and need to communicate between usercontrols through aspx page. 


Create a usercontrol with name usercontrol1 and having login screen with login button. The usercontrol having two textboxes one for username, and other for pasword and a textbox for login. 

<%@ Control Language="C#" AutoEventWireup="true" CodeBehind="UserControl1.ascx.cs"
    Inherits="testWebApp.UserControl1" %>
    <asp:TextBox ID="txtUserName" runat="server" />
    <asp:TextBox ID="txtPassword" runat="server" />
    <asp:Button ID="btnLogin" runat="server" onclick="btnLogin_Click" Text="Login" />

 UserControl 2

Create a usercontrol with name usercontrol2 and having single label control to display login name. 

<%@ Control Language="C#" AutoEventWireup="true" CodeBehind="UserControl2.ascx.cs" Inherits="testWebApp.UserControl2" %> 
<asp:Label ID="lblUserName" runat="server" Text="This is from UserControl2" />

 ASPX page

Create a aspx page with name main.aspx page and included above user controls and also created a label control for displaying current username

 <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="main.aspx.cs" Inherits="testWebApp.main" %>
 <%@ Register Src="~/UserControl1.ascx" TagName="uc1" TagPrefix="uc" %>
<%@ Register Src="~/UserControl2.ascx" TagName="uc2" TagPrefix="uc" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <form id="form1" runat="server">
        <asp:Label runat="server" ID="lblUserName" Text="This is from Aspx Page" />
        <uc:uc1 runat="server" ID="uc1" />
        <uc:uc2 runat="server" ID="uc2" />

 When we enter username and press enter, will raise a event from usercontrol to aspx page, means from usercontrol1 to main aspx. At the same time we need to display username in the aspx page. For achieving that first we store the username in the usercontrol itself as properties and raise a event from usercontrol to aspx page. To raise a event to parent page, declared a Event Handler and will call the event handler once the user press the button.

 Code behind of UserControl1 

public partial class UserControl1 : System.Web.UI.UserControl
        public EventHandler loginClickArgs;

        protected void Page_Load(object sender, EventArgs e)


        protected void btnLogin_Click(object sender, EventArgs e)
            UserName = txtUserName.Text;

        private string _username;
        public string UserName
            get { return _username; }
            set { _username = value; }

Codebehind main ASPX page 

To get event from usercontrol in aspx page, we need to initialise the event in the page load and create a funtion for curresponding event. The username entered in the usercontrol1 will get from the usercontrol1 properties. From here we assigned the label text by taking the value from usercontrol1.

     public partial class main : System.Web.UI.Page
        protected void Page_Load(object sender, EventArgs e)
            uc1.loginClickArgs += new EventHandler(login_Click);

        private void login_Click(object sender, EventArgs e)
            lblUserName.Text = "Hi " + uc1.UserName + ", Welcome to ASPX Page";
            uc2.UserName = uc1.UserName;

 Code behind userControl2

To call a function in usercontrol from aspx page, we need to declare a function as public in the usercontrol. If we need any dynamic parameter to the function, we need to create a properties in the usercontrol and set this from the aspx page. Here we are setting the username from the usercontrol1 to usercontrol2 from main aspx page. Then calling a public function displaymessage() in the usercontrol2 from the aspx page.

 public partial class UserControl2 : System.Web.UI.UserControl
        protected void Page_Load(object sender, EventArgs e)


        public void displayMessage()
            lblUserName.Text = "Hi " + UserName + ", This message from usercontrol";

        private string _username;
        public string UserName
            get { return _username; }
            set { _username = value; }

As a conclution, we have done following thing : we raise a event from a usercontrol to parent aspx page

(Event delegation from user control to aspx page in ASP.NET,C#), access the usercontrol event from aspx page, access the usercontrol variable from aspx page, call usercontrol’s public function from aspx page, setting a usercontrol’s variable from aspx page. That means we discussed all about usercontrol and aspx page communication.

  1. May 17, 2013 at 1:04 pm

    That’s like Houston Texas being without a Country station. Regular workshops ensure that the staff at PVM Radio subdivision is not only up to date with the latest technological innovations, but also ensures that the customers from diverse backgrounds are offered the best possible services to suit their tastes. Whats more is that 2G phones can come in a tinier and slimmer package, even its batteries.

  2. October 18, 2013 at 6:46 pm

    With applications like Viigo and Pocket Express, you can keep up to
    date on entertainment, sports, financial, political, U.

    There was something indescribable that seemed to call to me.
    Very few customers order anything larger than an
    8 by 10, and even in magazines most of the pictures are quite small.

  3. November 21, 2013 at 3:55 am

    Based on the information presented here, residents of Louisiana, whether they live and work
    in the countryside, or in massive cities such as New
    Orleans, can foist off the chains of dial-up
    Internet, DSL and Wi-Fi opting for a type of Internet service, which
    guarantees that the user connects to the web the first time every time.
    Link Building Services in Srilanka is one of the best services offered
    by the expert SEO team. For more information about the features and trial version of All – Proxy Lite follow the site:.

  1. July 5, 2014 at 3:25 pm

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: