C# .NET - 'System.Web.UI.WebControls.ListItem' in Assembly is not marked as serializable

Asked By MARKAND BHATT on 30-Jan-13 10:37 AM

I have made a property, which looks like below.


public ListItem[] DropDownListItems
    {
      get { return (ListItem[])ViewState["DropDownListItems"]; }
      set { ViewState["DropDownListItems"] = value; }
    }


And this is how i assign it values


ListItem[] litem = new ListItem[7];
litem[0] = new ListItem("View", "RowView");
litem[1] = new ListItem("ReadView", "RowReadView");
litem[2] = new ListItem("WriteView", "RowWriteView");
litem[3] = new ListItem("DeleteView", "RowDeleteView");
this.DropDownListItems=litem;


But I get the following error
'System.Web.UI.WebControls.ListItem' in Assembly is not marked as serializable


How to resolve it

Robbe Morris replied to MARKAND BHATT on 30-Jan-13 02:14 PM
This article may be of some help.  You are trying to use Serialization on a user interface control that does not implement the ISerializable interface.

http://www.eggheadcafe.com/tutorials/asp-net/1fde7294-de3c-45f7-8c4c-df711ff6e1e7/aspnet-dynamic-textbox--dropdownlist-control-viewstate-to-store-namevaluecollection.aspx