protected virtual string GenerateSelect(string target, object selectedValue, IEnumerable dataSource, IDictionary attributes)
{
string id = CreateHtmlId(target);
ApplyValidation(InputElementType.Select, target, ref attributes);
StringBuilder sb = new StringBuilder();
StringWriter sbWriter = new StringWriter(sb);
HtmlTextWriter writer = new HtmlTextWriter(sbWriter);
string firstOption = null;
string firstOptionValue = null;
string name = target;
if (attributes != null)
{
firstOption = CommonUtils.ObtainEntryAndRemove(attributes, "firstoption");
firstOptionValue = CommonUtils.ObtainEntryAndRemove(attributes, "firstoptionvalue");
if (attributes.Contains("name"))
{
name = (String) attributes["name"];
attributes.Remove("name");
}
if (attributes.Contains("id"))
{
id = (String) attributes["id"];
attributes.Remove("id");
}
}
OperationState state = SetOperation.IterateOnDataSource(selectedValue, dataSource, attributes);
writer.WriteBeginTag("select");
writer.WriteAttribute("id", id);
writer.WriteAttribute("name", name);
writer.Write(" ");
writer.Write(GetAttributes(attributes));
writer.Write(HtmlTextWriter.TagRightChar);
writer.WriteLine();
if (firstOption != null)
{
writer.WriteBeginTag("option");
writer.WriteAttribute("value", (firstOptionValue == null) ? "0" : SafeHtmlEncode(firstOptionValue));
writer.Write(HtmlTextWriter.TagRightChar);
writer.Write(SafeHtmlEncode(firstOption));
writer.WriteEndTag("option");
writer.WriteLine();
}
foreach(SetItem item in state)
{
writer.WriteBeginTag("option");
if (item.IsSelected)
{
writer.Write(" selected=\"selected\"");
}
writer.WriteAttribute("value", SafeHtmlEncode(item.Value));
writer.Write(HtmlTextWriter.TagRightChar);
writer.Write(SafeHtmlEncode(item.Text));
writer.WriteEndTag("option");
writer.WriteLine();
}
writer.WriteEndTag("select");
return sbWriter.ToString();
}