Click here to Skip to main content
15,885,716 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have made a property, which looks like below.

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

And this is how i assign it values

C#
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
Posted

1 solution

You get an error because ListItem is not natively serializable by the framework like Array, ArrayList and Hashtable.

Similar discussion at length: Persisting ListItemCollection values across postback, using ViewState[^]
It says:
1. ListItemCollection class is not marked as Serializable, so they can not be directly stored in ViewState collection(can not be persisted in any other persistent storage suc has file or database.......). The .net 2.0 ListItemCollection class implement the StateManager interface, we can call this method to let it help us do generate the data which can be persisted....

2. For the ListItemCollection, we can just override our custom control's LoadViewState and SaveViewState methods and manually store heir state value into ViewState collection...... (this is also what asp.net's buildin ListControl do in their implementation). In addition, we have to call the TraceViewState method(in IStateManager interface) when we created the ListItemCollection property's field
instance.... to make sure that our change on the ListItemCollection instance will be persisted....



Following MS Support article can also help: HOW TO: Serialize Web Server Controls by Using Visual C# .NET[^]
 
Share this answer
 
Comments
Espen Harlinn 30-Jan-13 18:41pm    
5'ed!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month


CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900