Creating Dynamic controls based on the selected value of a static control
<p>I have a drop down list holding some choices. Based on user selection I need to create some dynamic controls and render them on the form.</p>

<p>My understanding is that dynamic controls need to be created in OnInit or in CreateChildControls so that the ViewState for these dynamic controls is restored correctly by the runtime.</p>

<p>The problem is, I am unable to get the SelectedValue of the dropdown in OnInit or CreateChildControls since the ViewState has not been restored for the dropdown as yet. </p>

<p>Is there any way to obtain the current selection so that I can create the dynamic controls based on the current user selection and add them the page correctly</p>

<p>EDIT:
The markup looks as follows:</p>

<pre><code>&lt;form id="form1" runat="server"&gt;
&lt;div&gt;
    &lt;asp:DropDownList ID="ddl" runat="server" AutoPostBack="true"  AppendDataBoundItems="true"&gt;
        &lt;asp:ListItem Text="(Select Color)" Value="" /&gt;
        &lt;asp:ListItem Text="Red" Value="Red" /&gt;
        &lt;asp:ListItem Text="Green" Value="Green" /&gt;
        &lt;asp:ListItem Text="Blue" Value="Blue" /&gt;
    &lt;/asp:DropDownList&gt;
    &lt;asp:PlaceHolder ID="plHolder" runat="server" /&gt;
&lt;/div&gt;
&lt;/form&gt;
</code></pre>

<p>and here is the code behind:</p>

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

    }

    protected override void CreateChildControls()
    {
        base.CreateChildControls();
        TextBox tb = new TextBox();
        if (ddl.Text != "")
        {
            tb.Text = ddl.Text;
            if (Session["id"] != null)
            {
                string id = Session["id"].ToString();
                tb.ID = id;
            }
            else
            {
                Session["id"] = tb.ID = Guid.NewGuid().ToString().Replace("-", "");
            }
            plHolder.Controls.Add(tb);
        }

    }
}
</code></pre>

<p>On the line "tb.Text = ddl.Text;" I'm hoping to get the current selection and based on that set the value of the text property for the dynamic control. But the current selection has not been set yet since it's in OnInit.</p>
