C# String Library:
using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Text;
using System.Xml;
using System.Web.UI ;
using System.Web.UI.WebControls;
namespace CoreWebLibrary.Web.ControlUtilities
{
public static class ListControlHelper
{
public static void DoListControl(ListControl lc,
IList items)
{
DoListControl(lc, items, string.Empty, string.Empty, false);
}
public static void DoListControl(ListControl lc,
IList items, string textfield, string valuefield)
{
DoListControl(lc, items, textfield, valuefield, false);
}
public static void DoListControl(ListControl lc,
IList items, string textfield, string valuefield,
bool throwErrorOnNotFound, params string[] valuesToSelect)
{
DoListControl(lc, items, textfield, valuefield,
FindListItemBy.ByValue, throwErrorOnNotFound);
}
public static void DoListControl(ListControl lc,
IList items, string textfield, string valuefield,
FindListItemBy selectedType, bool throwErrorOnNotFound,
params string[] valuesToSelect)
{
lc.DataSource = items;
lc.DataTextField = textfield;
lc.DataValueField = valuefield;
lc.DataBind();
if (valuesToSelect.Length > 0)
ListControlHelper.Select(lc, throwErrorOnNotFound,
selectedType, valuesToSelect);
}
public static void DoListControl(ListControl lc,
IListSource items, string textfield, string valuefield)
{
DoListControl(lc, items, textfield, valuefield, false);
}
public static void DoListControl(ListControl lc,
IListSource items, string textfield, string valuefield,
bool throwErrorOnNotFound, params string[] valuesToSelect)
{
DoListControl(lc, items, textfield,
valuefield, FindListItemBy.ByValue, throwErrorOnNotFound);
}
public static void DoListControl(ListControl lc,
IListSource items, string textfield, string valuefield,
FindListItemBy selectedType, bool throwErrorOnNotFound,
params string[] valuesToSelect)
{
lc.DataSource = items;
lc.DataTextField = textfield;
lc.DataValueField = valuefield;
lc.DataBind();
if (valuesToSelect.Length > 0)
ListControlHelper.Select(lc, throwErrorOnNotFound,
selectedType, valuesToSelect);
}
public static void DoListControl(ListControl lc,
IDictionary items)
{
DoListControl(lc, items, "Value", "Key",
FindListItemBy.ByValue, false);
}
public static void DoListControl(ListControl lc,
IDictionary items, string textfield, string valuefield)
{
DoListControl(lc, items, textfield, valuefield,
FindListItemBy.ByValue, false);
}
public static void DoListControl(ListControl lc,
IDictionary items, string textfield, string valuefield,
bool throwErrorOnNotFound,
params string[] valuesToSelect)
{
DoListControl(lc, items, textfield, valuefield, FindListItemBy.ByValue,
throwErrorOnNotFound, valuesToSelect);
}
public static void DoListControl(ListControl lc,
IDictionary items, string textfield, string valuefield,
FindListItemBy selectedType, bool throwErrorOnNotFound,
params string[] valuesToSelect)
{
lc.DataSource = items;
lc.DataTextField = textfield;
lc.DataValueField = valuefield;
lc.DataBind();
if (valuesToSelect.Length > 0)
ListControlHelper.Select(lc, throwErrorOnNotFound,
selectedType, valuesToSelect);
}
public static void ApplyListItemStyles(ListControl lc,
IDictionary<HtmlTextWriterStyle, string> styles,
params string[] valuesToApplyTo)
{
foreach (string s in valuesToApplyTo)
{
ListItem item = lc.Items.FindByValue(s);
if (item != null)
{
CssStyleCollection css = item.Attributes.CssStyle ;
foreach (KeyValuePair<HtmlTextWriterStyle, string> s in styles)
{
css.Add(s.Key, s.Value);
}
}
}
}
public static void ApplyListItemStyles(ListControl lc,
IDictionary<HtmlTextWriterStyle, string> styles,
params ListItem[] itemsToApplyTo)
{
foreach (ListItem item in itemsToApplyTo)
{
CssStyleCollection css = item.Attributes.CssStyle;
foreach (KeyValuePair<HtmlTextWriterStyle, string> s in styles)
{
css.Add(s.Key, s.Value);
}
}
}
public static void SelectByValue(ListControl lc,
params string[] valuesToSelect)
{
ListControlHelper.Select(lc, false,
FindListItemBy.ByValue, valuesToSelect);
}
public static void SelectByValue(ListControl lc,
bool throwErrorOnNotFound,
params string[] valuesToSelect)
{
ListControlHelper.Select(lc, throwErrorOnNotFound,
FindListItemBy.ByValue, valuesToSelect);
}
public static void SelectByText(ListControl lc,
params string[] valuesToSelect)
{
ListControlHelper.Select(lc, false,
FindListItemBy.ByText, valuesToSelect);
}
public static void SelectByText(ListControl lc,
bool throwErrorOnNotFound,
params string[] valuesToSelect)
{
ListControlHelper.Select(lc, throwErrorOnNotFound,
FindListItemBy.ByText, valuesToSelect);
}
public static string SelectedItemsValueToCSV(ListControl lc)
{
StringBuilder sb = new StringBuilder();
foreach (ListItem item in lc.Items)
{
if (item.Selected)
sb.AppendFormat("{0},", item.Value);
}
return sb.ToString().TrimEnd(',');
}
public static string SelectedItemsTextToCSV(ListControl lc)
{
StringBuilder sb = new StringBuilder();
foreach (ListItem item in lc.Items)
{
if (item.Selected)
sb.AppendFormat("{0},", item.Text);
}
return sb.ToString().TrimEnd(',');
}
public static string SelectedItemsToXML(ListControl lc, string nodeName)
{
StringBuilder sb = new StringBuilder();
sb.AppendFormat("<{0}>", nodeName);
foreach (ListItem item in lc.Items)
{
if (item.Selected)
{
sb.AppendFormat("<item text=\"{0}\" value=\"{1}\"
selected=\"true\" />", item.Text, item.Value);
}
}
sb.AppendFormat("</{0}>", nodeName);
return sb.ToString();
}
public static void ClearAllSelectedItems(ListControl lc)
{
if (lc is DropDownList)
{
if (lc.SelectedItem != null)
lc.SelectedItem.Selected = false;
}
else
{
foreach (ListItem item in lc.Items)
{
item.Selected = false;
}
}
}
#region Private Methods
private static void Select(ListControl lc,
bool throwErrorOnNotFound,
FindListItemBy findBy,
params string[] valuesToSelect)
{
foreach (string s in valuesToSelect)
{
ListItem item = ItemFindBy(lc, s, findBy);
if (item != null)
{
item.Selected = true;
}
else if (throwErrorOnNotFound)
{
throw new ApplicationException(
string.Format("Value: {0} not found in ListControl {1}",
s, lc.ID));
}
}
}
private static ListItem ItemFindBy(ListControl lc,
string value,
FindListItemBy findBy)
{
if (findBy == FindListItemBy.ByText)
return lc.Items.FindByText(value);
if (findBy == FindListItemBy.ByValue)
return lc.Items.FindByValue(value);
return null;
}
#endregion
#region Public Enum
public enum FindListItemBy
{
ByValue,
ByText
}
#endregion
}
}