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[
^]