Click here to Skip to main content
Click here to Skip to main content

Fire dynamically loaded UserControl event on the page

, 18 Sep 2012 CPOL
Rate this:
Please Sign up or sign in to vote.
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.

<%@ 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

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

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

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)

Share

About the Author

Sudip Saha
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
Group type: Organisation

16 members


Comments and Discussions

 
QuestionButton Click Event is not fired PinmemberRakesh Jogani20-Nov-13 0:37 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Terms of Use | Mobile
Web03 | 2.8.1411023.1 | Last Updated 18 Sep 2012
Article Copyright 2012 by Sudip Saha
Everything else Copyright © CodeProject, 1999-2014
Layout: fixed | fluid