title: How to create a html select in code behind c#?
body: <p>We need to create html dropdown list dynamically. Following is our current code. Option1 and Option2 are strings that include all the items. The reason we have two strings is we split all the items in two groups. </p>
  
  <pre><code>&lt;td&gt;
  &lt;div id="dvGroup" style="display:none;" runat="server"&gt;&lt;/div&gt;
  &lt;/td&gt;
  
  string Option1 = "&lt;optgroup label='Group1'&gt;&lt;option&gt;1&lt;/option&gt;&lt;option1&gt;2&lt;/option&gt;";
  string Option2 = "&lt;optgroup label='Group2'&gt;&lt;option&gt;a&lt;/option&gt;&lt;option&gt;b&lt;/option&gt;&lt;option&gt;c&lt;/option&gt;;
  …
  dvGroup.InnerHtml = "&lt;Select name='ddGroup' id='ddGroup'&gt;" + Option1 + Option2 + "&lt;/Select&gt;";
  </code></pre>
  
  <p>But by setting InnerHtml will potentially bring some security problem. So I changed it to:</p>
  
  <pre><code>HtmlSelect select = new HtmlSelect();
  select.ID = "ddGroup";
  select.Name = "ddGroup";
  select.ClientIDMode = ClientIDMode.Static;
  dvGroup.Controls.Add(select);
  
  select.Controls.Add(new LiteralControl(Option1 + Option2));
  </code></pre>
  
  <p>However, it threw following error at the line select.Controls.Add…</p>
  
  <pre><code>'System.Web.UI.HtmlControls.HtmlSelect' does not allow child controls. 
  </code></pre>
  
  <p>How do I fix this? Another problem is, setting ClientIDMode to Static only make sure the id is not changed, but name still being added extra stuff. How do I make sure name is not changed either?</p>