private static string ConstructXmlFromDropDownItems(ListItemCollection dropdownitems, string outerTagName, string innerTagName) { var outerOpenTag = string.Format("<{0}>", outerTagName); var outerCloseTag = string.Format("</{0}>", outerTagName); var innerOpenTag = string.Format("<{0}>", innerTagName); var innerCloseTag = string.Format("</{0}>", innerTagName); var xml = outerOpenTag; foreach (string itemIdString in dropdownitems.OfType <ListItem>().Where(x => x.Selected).Select(x => x.Value.Split(',')[0])) { var itemID = int.Parse(itemIdString.Substring(itemIdString.IndexOf('=') + 1)); xml += innerOpenTag; xml += "<id>" + itemID + "</id>"; xml += innerCloseTag; } xml += outerCloseTag; return(xml); }
private void RenderContentsAutoGrouped(HtmlTextWriter writer) { if (HttpContext.Current == null) { return; } ListItemCollection items = this.Items; int count = items.Count; var groupedItems = items.OfType <ListItem>().GroupBy(x => x.Attributes["Group"] ?? String.Empty).Select(x => new { Group = x.Key, Items = x.ToList() }); if (count > 0) { Boolean flag = false; foreach (var groupedItem in groupedItems) { if (string.IsNullOrEmpty(groupedItem.Group) == false) { writer.WriteBeginTag("optgroup"); writer.WriteAttribute("label", groupedItem.Group); writer.Write('>'); } for (int i = 0; i < groupedItem.Items.Count; ++i) { ListItem item = groupedItem.Items[i]; if (item.Enabled == true) { writer.WriteBeginTag("option"); if (item.Selected == true) { if (flag == true) { this.VerifyMultiSelect(); } flag = true; writer.WriteAttribute("selected", "selected"); } writer.WriteAttribute("value", item.Value, true); if (item.Attributes.Count != 0) { item.Attributes.Render(writer); } if (this.Page != null) { this.Page.ClientScript.RegisterForEventValidation(this.UniqueID, item.Value); } writer.Write(Html32TextWriter.TagRightChar); if (htmlEncodeItems) { HttpUtility.HtmlEncode(item.Text, writer); } else if (htmlAttributeEncodeItems) { HttpUtility.HtmlAttributeEncode(item.Text, writer); } else { writer.Write(item.Text); } writer.WriteEndTag("option"); writer.WriteLine(); } } if (string.IsNullOrEmpty(groupedItem.Group) == false) { writer.WriteEndTag("optgroup"); } } } }
public static IEnumerable <ListItem> GetSelectedItems(this ListItemCollection items) { return(items.OfType <ListItem>().Where(item => item.Selected)); }
protected override void RenderContents(System.Web.UI.HtmlTextWriter writer) { // The current control being "adaptered" is available within context from the Control property DropDownList dropDownList = (DropDownList)Control; ListItemCollection items = dropDownList.Items; // Retrieve Optgrouping using LinQ var groups = (from p in items.OfType <ListItem>() group p by p.Attributes["Group"] into g select new { Label = g.Key, Items = g.ToList <ListItem>() }); foreach (var group in groups) { if (!String.IsNullOrEmpty(group.Label)) { writer.WriteBeginTag("optgroup"); writer.WriteAttribute("label", group.Label); writer.Write(">"); } int count = group.Items.Count(); if (count > 0) { bool flag = false; for (int i = 0; i < count; i++) { ListItem item = group.Items[i]; writer.WriteBeginTag("option"); if (item.Selected) { if (flag) { throw new HttpException("Multiple selected items not allowed"); } flag = true; writer.WriteAttribute("selected", "selected"); } if (!item.Enabled) { writer.WriteAttribute("disabled", "true"); } writer.WriteAttribute("value", item.Value, true); if (this.Page != null) { this.Page.ClientScript.RegisterForEventValidation(dropDownList.UniqueID, item.Value); } writer.Write('>'); HttpUtility.HtmlEncode(item.Text, writer); writer.WriteEndTag("option"); writer.WriteLine(); } } if (!String.IsNullOrEmpty(group.Label)) { writer.WriteEndTag("optgroup"); } } }
protected override void RenderContents(HtmlTextWriter writer) { ListItemCollection items = this.Items; Int32 count = items.Count; var groupedItems = items.OfType <ListItem>().GroupBy(x => x.Attributes["Group"] ?? String.Empty).Select(x => new { Group = x.Key, Items = x.ToList() }); if (count > 0) { Boolean flag = false; foreach (var groupedItem in groupedItems) { if (String.IsNullOrWhiteSpace(groupedItem.Group) == false) { writer.WriteBeginTag("optgroup"); writer.WriteAttribute("label", groupedItem.Group); writer.Write('>'); } for (Int32 i = 0; i < groupedItem.Items.Count; ++i) { ListItem item = groupedItem.Items[i]; if (item.Enabled == true) { writer.WriteBeginTag("option"); if (item.Selected == true) { if (flag == true) { this.VerifyMultiSelect(); } flag = true; writer.WriteAttribute("selected", "selected"); } writer.WriteAttribute("value", item.Value, true); if (item.Attributes.Count != 0) { item.Attributes.Render(writer); } if (this.Page != null) { this.Page.ClientScript.RegisterForEventValidation(this.UniqueID, item.Value); } writer.Write('>'); HttpUtility.HtmlEncode(item.Text, writer); writer.WriteEndTag("option"); writer.WriteLine(); } } if (String.IsNullOrWhiteSpace(groupedItem.Group) == false) { writer.WriteEndTag("optgroup"); } } } }