How can I add a dynamic numer of UserControls in an asp.net page?
<p>Here's the problem:</p>

<p>I have an (almost) empty aspx page and I want to insert a certain number of the same user control in this page.</p>

<p>The fact is that I don't know how many of them I will need.</p>

<p>I tried to add them from the CodeBehind but it seems that the UserControls are completely empty.</p>

<p>In the main page (MainDiv is a div with runat="server"):</p>

<pre><code>protected void Page_Init(object sender, EventArgs e)
{
    WebUserControl1 uc = new WebUserControl1();
    WebUserControl1 uc1 = new WebUserControl1();
    MainDiv.Controls.Add(uc);
    MainDiv.Controls.Add(uc1);
}
</code></pre>

<p>(it doesn't work if i put this code on Page_Init, Page_Load or Page_PreRender)</p>

<p>UserControl (gw is a Gridview contained in the UserControl):</p>

<pre><code>protected void Page_PreRender(object sender, EventArgs e)
{
    if (_data != null)
    {
        gw.DataSource = _data;
        gw.DataBind();
    }
}
</code></pre>

<p>when I arrive there, gw is null (this.Controls.Count is 0).</p>

<p>How can I solve this?</p>
