title: Add dynamic ASP.NET controls via a DropDownList to the page
body: <p><strong>- Primary Info:</strong><br>
  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'<br>
  'Between 10 and 30'<br>
  'more than 30'<br>
  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)<br>
  <strong>- What I do:</strong><br>
  I create a dropDownList and set it's <em>AutoPostBack</em> property to <em>true</em> 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:</p>
  
  <pre><code>protected void DropDownListColumnNameSelectedIndexChanged(object sender, EventArgs e)
      {
          Cache["SelectedKey"] = dropDownListColumnName.SelectedValue;
      }
  </code></pre>
  
  <p>When user select an item from dropDownList *DropDownList_SelectedIndexChanged* fire, and I must create controls dynamically in a place holder:  </p>
  
  <pre><code> var textBoxName = new TextBox
      {
          ID = "textBoxName",
          CssClass = "str-search-textbox-highlight",
          ViewStateMode = ViewStateMode.Disabled
      };
   placeHolderFirstItem.Controls.Add(textBoxName);
  </code></pre>
  
  <p><strong>- What is the problem?</strong>
  When I try add new control in current Button_Click event, control added successfully to page but I can't find it by <em>placeHolderFirstItem.Controls.Find("textBoxName")</em> actually <strong>placeHolderFirstItem.Controls.Count</strong> is always zero. So I can't get textBoxName.Text  values.<br>
  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):  </p>
  
  <pre><code>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);
          }
  </code></pre>
  
  <p>after doing this I can find "textBoxName" in placeHolderFirstItem, but it fire before <strong>DropDownList_SelectedIndexChanged</strong> !<br>
  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?<br>
  Thanks in advance,<br>
  Mohsen. </p>
  
  <p><strong>- Updated:</strong><br>
  <strong>Here is the better solution</strong><br>
  (<a href="http://forums.asp.net/p/1959726/5596531.aspx?p=True&amp;t=635244790943067485&amp;pagenum=1" rel="nofollow">http://forums.asp.net/p/1959726/5596531.aspx?p=True&amp;t=635244790943067485&amp;pagenum=1</a>)</p>