I needed a listbox with groups and for that I placed a literal control in page and I rendered a select control using HTMLTextWriter to the literal control. It rendered perfectly.
Problem is I am not able to get the select control from Request.Form object. The id of select control is missing in Request.Form.
Why is it so and how can I get the user selections in code behind? Code given below.
Dictionary<string,List<string>> locations = GetLocations();
StringWriter stringWriter = new StringWriter();
HtmlTextWriter html = new HtmlTextWriter(stringWriter);
html.AddAttribute(HtmlTextWriterAttribute.Multiple, "multiple");
html.AddAttribute(HtmlTextWriterAttribute.Id, "ddlLocations");
html.RenderBeginTag(HtmlTextWriterTag.Select);
foreach (string region in locations.Keys)
{
html.AddAttribute("label", region);
html.RenderBeginTag("optgroup");
//add countries
foreach (string country in locations[region])
{
html.AddAttribute(HtmlTextWriterAttribute.Value, country);
html.RenderBeginTag(HtmlTextWriterTag.Option);
html.Write(country);
html.RenderEndTag();//close option
}
html.RenderEndTag();//close optgroup
}
html.RenderEndTag();//close select
literalForLocationDdl.Text = stringWriter.ToString();