Dynamic controls: associated events don't fire if recreated during Page_Load
<p><em>Disclaimer: I have read <a href="http://msdn.microsoft.com/en-us/library/ms178472.aspx" rel="nofollow">the ASP.net page life cycle</a>, and I've also read <a href="http://msdn.microsoft.com/en-us/library/aa479330.aspx" rel="nofollow">several</a> <a href="http://www.4guysfromrolla.com/articles/081402-1.aspx" rel="nofollow">good</a> <a href="http://www.singingeels.com/Articles/Dynamically_Created_Controls_in_ASPNET.aspx" rel="nofollow">articles</a> pertaining to dynamic controls, but I must be missing something.</em></p>

<p><strong>Background:</strong> I am currently working on a website which needs to create a lot of dynamic content based on the user's input. I understand that, in order for dynamic controls to persist and for their events to wire up correctly, I need to re-create those dynamic controls on every page post-back.</p>

<p>Because of the nature of my project, my code doesn't know WHAT controls to create unless it has some information about what the user selected. I'm storing the user's choices in the <code>ViewState</code>, which is unavailable in <code>Page_Init</code> because it has not yet loaded. Consequently, I have to wait until <code>Page_PreLoad</code> or <code>Page_Load</code> to read the ViewState and then re-create my dynamic controls.</p>

<p><strong>The part I don't understand:</strong> When I try re-creating my controls during <code>Page_Load</code>, the controls persist but the associated events don't seem to fire. For example, clicking on a <code>LinkButton</code> I created does not fire the method that I wired to its <code>Click</code> event, even though the button itself does persist. </p>

<p>A strange solution that I discovered by accident is that I can instead re-create my controls during <code>Page_PreLoad</code> and then the events fire correctly.</p>

<p><strong>My question (or questions, rather):</strong> Why does my problem appear to go away by re-creating my controls during <code>Page_PreLoad</code> instead of <code>Page_Load</code>? Is this a safe practice? I've never seen any other code that used <code>Page_PreLoad</code>, which makes me wary. This solution is working for me, but are there any pitfalls I might be missing? Am I unknowingly setting myself up for failure later on?</p>

<p>My code, where <code>LoadDocument()</code> is a method that creates controls and stuffs them into a static <code>Panel</code> control:</p>

<pre><code>protected void Page_PreLoad(object sender, EventArgs e)
{
    if (ViewState["xmlfilename"] != null)
    {
        LoadDocument(ViewState["xmlfilename"].ToString());
    }
}
</code></pre>
