Click here to Skip to main content
13,457,673 members
Click here to Skip to main content
Add your own
alternative version


2 bookmarked
Posted 18 Sep 2012

Fire dynamically loaded UserControl event on the page

, 18 Sep 2012
Rate this:
Please Sign up or sign in to vote.
Load usercontrol dynamically and fire usercontrol's button event on content page.


We can add User control on the page by using reference or by dynamically loaded it on the page  in some case we require to fire usercontrol's event like button click on the content page.That we can do using custom event.

here my sample example is how we can fire dynamically loaded user control's button event on the content page. In my simple example I have added a content page and a usercontrol.

In UserControl.ascx,I have added two text box controls, one for "user name" and another for "password" and a login button.

<%@ Control Language="C#" AutoEventWireup="true" 

    CodeFile="MyUserControl.ascx.cs" Inherits="MyUserControl" %>
<tr> <td> User Name</td> <td> 

<asp:TextBox ID="txtUserName"runat="server"></asp:TextBox> </td> 

<td> Password</td> <td> <asp:TextBox ID="txtPassword" 

   runat="server"></asp:TextBox> </td> </tr> 

 <td> </td> <td> <asp:Button ID="btnSave" 

    runat="server" Text="Login" 

    onclick="btnSave_Click" /> </td> </tr> </table> 


Declare an event login and subscribe the event on button click

public partial class MyUserControl : System.Web.UI.UserControl
    public event EventHandler login;
    protected void Page_Load(object sender, EventArgs e)
    protected void btnSave_Click(object sender, EventArgs e)
        if (login != null)
            login(sender, e);

Now "MyUserControl" add on the content page dynamically.


<form id="form1" runat="server">

<div id="divlogin" runat="server">



<div id="divlogin" runat="server">

In code behind DynamicallyLoadUserControl.aspx.cs write the below code.

public partial class DynamicallyLoadUserControl : System.Web.UI.Page
    protected void Page_Load(object sender, EventArgs e)
        var ucon = Page.LoadControl("~/MyUserControl.ascx");
        ucon.ID = "UserControl1";
        dynamic d = ucon;//element that is typed as dynamic is assumed to support any operation
        d.login += new EventHandler(UserControl1_login);
    void UserControl1_login(object sender, EventArgs e)
        UserControl UserControl1 = (UserControl)Page.FindControl("UserControl1");
        TextBox txt1 = (TextBox)UserControl1.FindControl("txtUserName");
        TextBox txt2 = (TextBox)UserControl1.FindControl("txtPassword");
        Response.Write(string.Format("UserName {0} Passwor {1}", txt1.Text, txt2.Text));


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

Sudip Saha
Web Developer
India India
This member doesn't quite have enough reputation to be able to display their biography and homepage.
Group type: Organisation

24 members

You may also be interested in...

Comments and Discussions

QuestionButton Click Event is not fired Pin
Rakesh Jogani19-Nov-13 23:37
memberRakesh Jogani19-Nov-13 23:37 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    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 | Terms of Use | Mobile
Web01 | 2.8.180322.1 | Last Updated 18 Sep 2012
Article Copyright 2012 by Sudip Saha
Everything else Copyright © CodeProject, 1999-2018
Layout: fixed | fluid