Add dynamic ASP.NET controls via a DropDownList to the page

- Primary Info:
In my recent project, I need to have a page with a DropDownList with some items like 'firstName' , 'lastName' , 'Age' and etc. I want to add optional controls to the page when every item selected by user. For example when user select the 'Age' another dropdownlist created dynamically with these values : 'Less than 10'
'Between 10 and 30'
'more than 30'
Here is a button that add this user selection to listBox and let user to choice another options. (I made a query at last according to user choices and send it to db)
- What I do:
I create a dropDownList and set it's AutoPostBack property to true and adds some items in it and user must select one of those item. then I add user SelectedValue of dropDownList in a Cache variable before page post back happens:

protected void DropDownListColumnNameSelectedIndexChanged(object sender, EventArgs e)
    {
        Cache["SelectedKey"] = dropDownListColumnName.SelectedValue;
    }

When user select an item from dropDownList *DropDownList_SelectedIndexChanged* fire, and I must create controls dynamically in a place holder:

 var textBoxName = new TextBox
    {
        ID = "textBoxName",
        CssClass = "str-search-textbox-highlight",
        ViewStateMode = ViewStateMode.Disabled
    };
 placeHolderFirstItem.Controls.Add(textBoxName);

- What is the problem? When I try add new control in current Button_Click event, control added successfully to page but I can't find it by placeHolderFirstItem.Controls.Find("textBoxName") actually placeHolderFirstItem.Controls.Count is always zero. So I can't get textBoxName.Text values.
I try to google that for any solution and I found some solution that I must add controls in Page.OnInit so I add controls in overridden OnInit(e):

protected override void OnInit(EventArgs e)
        {
            if (!Page.IsPostBack) return;
            var textBoxName = new TextBox
               {
                   ID = "textBoxName",
                   CssClass = "str-search-textbox-highlight",
                   ViewStateMode = ViewStateMode.Disabled
               };
            placeHolderFirstItem.Controls.Add(textBoxName);
        }

after doing this I can find "textBoxName" in placeHolderFirstItem, but it fire before DropDownList_SelectedIndexChanged !
so how can I add new controls to place holder exactly when user change the dropDownList value and how can I read new controls value?
Thanks in advance,
Mohsen.

- Updated:
Here is the better solution
()


ANSWERS:


When you are dynamically adding controls, you have to reload the controls into the control tree everytime thereafter for it to appear. With the help of viewstate, you could change your code sample to have:

ViewState("ShowTextbox") = true

And then in your init routine:

protected override void OnInit(EventArgs e)
    {
        if (!Page.IsPostBack) return;

        if (ViewState("ShowTextBox") == true) {
           var textBoxName = new TextBox
           {
               ID = "textBoxName",
               CssClass = "str-search-textbox-highlight",
               ViewStateMode = ViewStateMode.Disabled
           };
           placeHolderFirstItem.Controls.Add(textBoxName);
        }
    }

Please note it's much easier to have a control on the control tree, and then show/hide by setting Visible to true/false, because of these ASP.NET control tree issues.



 MORE:


 ? WinForm: control won't add to a panel
 ? ASP.NET refresh Update Panel
 ? Create textbox dynamically without postback
 ? Second button Event is not happening in Update panel in aspx page
 ? ASP.Net: click event for dynamic button in dynamically created nested update panel is not raised
 ? button click event on update panel not firing in multiple user control
 ? add a panel or label control dynamically to a datagrid
 ? wxPython - problem with nested sizers, a panel and SetSizer / Fit
 ? ExtJS add panel to panel
 ? ExtJS Add panel above already added panel