Processing events on Dynamically Loaded User Controls
<p>I have a number of pages that I need to Dynamically Load User Controls and process events on controls on them. I have included sample code below for a trivial example.</p>

<p>Default.aspx</p>

<pre><code>&lt;%@ Page Language="C#" AutoEventWireup="true"  CodeFile="Default.aspx.cs" Inherits="_Default" %&gt;

&lt;!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"&gt;

&lt;html xmlns="http://www.w3.org/1999/xhtml"&gt;
&lt;head runat="server"&gt;
    &lt;title&gt;&lt;/title&gt;
&lt;/head&gt;
&lt;body&gt;
    &lt;form id="form1" runat="server"&gt;
    &lt;div&gt;
    &lt;asp:Button ID="BtnLoadControl" runat="server" Text="Load Control 1" 
            onclick="BtnLoadControl_Click" /&gt;&lt;br /&gt;
    &lt;asp:Button ID="BntLoadControl2" runat="server" Text="Load Control 2" 
            onclick="BntLoadControl2_Click" /&gt;
    &lt;asp:PlaceHolder ID="ControlArea" runat="server" /&gt;
    &lt;/div&gt;
    &lt;/form&gt;
&lt;/body&gt;
&lt;/html&gt;
</code></pre>

<p>Deafualt.aspx.cs</p>

<pre><code>    using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class _Default : System.Web.UI.Page 
{
    protected void Page_Load(object sender, EventArgs e)
    {

    }
    protected void BtnLoadControl_Click(object sender, EventArgs e)
    {
        Control controlToAdd = Page.LoadControl("~/control1.ascx");
        this.ControlArea.Controls.Add(controlToAdd);
    }
    protected void BntLoadControl2_Click(object sender, EventArgs e)
    {
        Control controlToAdd = Page.LoadControl("~/control2.ascx");
        this.ControlArea.Controls.Add(controlToAdd);
    }
}
</code></pre>

<p>Control1.ascx</p>

<pre><code>&lt;%@ Control Language="C#" AutoEventWireup="true" CodeFile="control1.ascx.cs" Inherits="control1" %&gt;
&lt;div style="border: 1px solid red;"&gt;Test Control
&lt;br /&gt;&lt;asp:Button runat="server" ID="testButton" Text="test" onclick="testButton_Click" /&gt;&lt;/div&gt;
</code></pre>

<p>Control1.ascx.cs</p>

<pre><code>using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

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

    }

    protected void testButton_Click(object sender, EventArgs e)
    {
        Response.Write("Button Clicked on Control 1!");
    }
}
</code></pre>

<p>Control2.ascx</p>

<pre><code>&lt;%@ Control Language="C#" AutoEventWireup="true" CodeFile="Control2.ascx.cs" Inherits="Control2" %&gt;
&lt;div style="border: 1px solid red;"&gt;Test Control 2
&lt;br /&gt;&lt;asp:Button runat="server" ID="testButton2" Text="test me" 
        onclick="testButton2_Click"  /&gt;&lt;/div&gt;
</code></pre>

<p>Control2.ascx.cs</p>

<pre><code>using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

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

    }
    protected void testButton2_Click(object sender, EventArgs e)
    {
        Response.Write("Button Clicked on Control 2!");
    }
}
</code></pre>

<p>This is a very trivial example to show what I would like to do. My actual control is more complex and has <strong>several properties that I set for the control I load</strong> as I load it (in the default.aspx.cs files event handler).</p>

<p>With this example what happens is when the button is clicked on one of the loaded controls, the page is re-loaded and the control is no longer there. How can I keep the control loaded as well as process any events that happen on the control?</p>
