using System; using System.Collections.Generic; using System.Collections.ObjectModel; using System.Web.UI.WebControls; using System.Collections; namespace System.Web.Script.Serialization.CS { public class ListItemCollectionConverter : JavaScriptConverter { public override IEnumerable SupportedTypes { //Define the ListItemCollection as a supported type. get { return new ReadOnlyCollection(new List(new Type[] { typeof(ListItemCollection) })); } } public override IDictionary Serialize(object obj, JavaScriptSerializer serializer) { ListItemCollection listType = obj as ListItemCollection; if (listType != null) { // Create the representation. Dictionary result = new Dictionary(); ArrayList itemsList = new ArrayList(); foreach (ListItem item in listType) { //Add each entry to the dictionary. Dictionary listDict = new Dictionary(); listDict.Add("Value", item.Value); listDict.Add("Text", item.Text); itemsList.Add(listDict); } result["List"] = itemsList; return result; } return new Dictionary(); } public override object Deserialize(IDictionary dictionary, Type type, JavaScriptSerializer serializer) { if (dictionary == null) throw new ArgumentNullException("dictionary"); if (type == typeof(ListItemCollection)) { // Create the instance to deserialize into. ListItemCollection list = new ListItemCollection(); // Deserialize the ListItemCollection's items. ArrayList itemsList = (ArrayList)dictionary["List"]; for (int i=0; i(itemsList[i])); return list; } return null; } } }