Click here to Skip to main content
15,884,353 members
Articles / Web Development / ASP.NET
Tip/Trick

Fire dynamically loaded UserControl event on the page

Rate me:
Please Sign up or sign in to vote.
4.50/5 (2 votes)
18 Sep 2012CPOL 26.2K   2   1
Load usercontrol dynamically and fire usercontrol's button event on content page.

Introduction

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.

ASP.NET
<%@ Control Language="C#" AutoEventWireup="true" 
    CodeFile="MyUserControl.ascx.cs" Inherits="MyUserControl" %>
<table> 
<tr> <td> User Name</td> <td> 

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

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


<tr>
 <td> </td> <td> <asp:Button ID="btnSave" 
    runat="server" Text="Login" 
    onclick="btnSave_Click" /> </td> </tr> </table> 

MyUserControl.ascx.cs

Declare an event login and subscribe the event on button click

C#
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.

DynamicallyLoadUserControl.aspx

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

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

</div>

</form>

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

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

C#
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
        divlogin.Controls.Add(d);
        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));
    }
}

License

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


Written By
Web Developer
India India
Know Me Know Fun
No Me No Fun
I am MCTS,MCP certified software developer
having 6 year experience on ASP.NET,WCF,C#,SQLSERVER,MVC,JSON,JAVASCRIPT,
JqueryMobile,HTML5
http://www.Oplaner.com

sudip.rec@gmail.com
This is a Organisation

24 members

Comments and Discussions

 
QuestionButton Click Event is not fired Pin
Rakesh Jogani19-Nov-13 23:37
professionalRakesh 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.