Click here to Skip to main content
13,146,014 members (44,037 online)
Rate this:
Please Sign up or sign in to vote.
See more:
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");

But I get the following error

'System.Web.UI.WebControls.ListItem' in Assembly is not marked as serializable
How to resolve it
Posted 30-Jan-13 4:38am

1 solution

Rate this: bad
Please Sign up or sign in to vote.

Solution 1

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'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[^]
Espen Harlinn 30-Jan-13 18:41pm

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

Advertise | Privacy |
Web03 | 2.8.170915.1 | Last Updated 30 Jan 2013
Copyright © CodeProject, 1999-2017
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100