我需要一个带有组的列表框,为此我在页面中放置了一个文字控件,然后使用HTMLTextWriter将一个选择控件呈现给文字控件。它完美呈现。
问题是我无法从Request.Form对象获取select控件。 Request.Form中缺少select控件的id。
为什么会如此?如何在代码中获取用户选择?代码如下。
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();