Dynamically added user control event not firing
<p>I saw that there are many similar question being asked. But in my case, I don't want add the user control on Page_Load nor OnInit. I would like to dynamically add user control to a PlaceHolder when a button on the page is clicked. I am able to load the control and it displays just fine. But the buttons in that user control wouldn't work. The event handler wouldn't fire. Worse than that, the whole user control would disappear after clicking the button in the user control. </p>

<p>Here is my user control which has a "RefreshButton" and its event handler simply updates the MessageLabel with DateTime.Now.</p>

<p>------------------- TestUC.ascx ------------------------------
<code></p>

<pre><code>&lt;%@ Control Language="C#" AutoEventWireup="true" CodeBehind="TestUC.ascx.cs" Inherits="PASS.Web.UserControls.TestUC" %&gt;
TestUC: 
&lt;asp:Label ID="MessageLabel" runat="server"&gt;&lt;/asp:Label&gt;
&lt;asp:Button ID="RefreshButton" runat="server" Text="Refresh" onclick="RefreshButton_Click" /&gt;
</code></pre>

<p></code></p>

<p>---------------- TestUC.ascx.cs ------------------------------</p>

<p><code></p>

<pre><code>protected void Page_Load(object sender, EventArgs e)
{ }

protected void RefreshButton_Click(object sender, EventArgs e)
{
    this.MessageLabel.Text = " Server Time: " + DateTime.Now.ToString();
}
</code></pre>

<p></code></p>

<p>The parent page has a AddButton and a place holder. An instance of TestUC.ascx is (supposed to be) created every time the AddButton is clicked.</p>

<p>------------ Test.aspx ----------------
<code></p>

<pre><code>&lt;asp:Panel ID="DynamicUserControlPanel" runat="server"&gt;
    &lt;asp:Button ID="AddButton" runat="server"Text="Add New User Control Dynamically" onclick="AddButton_Click" /&gt;
    &lt;asp:Panel ID="PlaceHolder" runat="server"&gt;&lt;/asp:Panel&gt;
&lt;/asp:Panel&gt;
</code></pre>

<p></code></p>

<p>----------- Test.aspx.cs ----------------</p>

<p><code></p>

<pre><code>protected void AddButton_Click(object sender, EventArgs e)
{
    UserControls.TestUC uc = LoadControl("~/UserControls/TestUC.ascx") as UserControls.TestUC;
    uc.ID = RandomStringGenerator.Generate(10);
    this.PlaceHolder.Controls.Add(uc);
}
</code></pre>

<p></code></p>

<p>First of all, when AddButton is clicked (and keep being clicked), only one instance of TestUC.ascx is added and displayed. Secondly, the RefreshButton in the user control wouldn't fire. Thirdly, the user control would disappear when RefreshButton is clicked.</p>

<p>Is the idea of dynamically adding user control on button click feasible at all? Can someone please provide a simply example for achieving that?</p>

<p>Thank you very much in advance.</p>
