Click here to Skip to main content
11,583,865 members (62,665 online)
Rate this: bad
Please Sign up or sign in to vote.
See more: C# ASP.NET
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 at 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
0 OriginalGriff 440
1 Sergey Alexandrovich Kryukov 310
2 virusstorm 268
3 Richard Deeming 220
4 Dave Kreskowiak 200
0 OriginalGriff 1,623
1 Sergey Alexandrovich Kryukov 1,135
2 Abhinav S 701
3 Dave Kreskowiak 627
4 Suvendu Shekhar Giri 568

Advertise | Privacy | Mobile
Web04 | 2.8.150603.1 | Last Updated 30 Jan 2013
Copyright © CodeProject, 1999-2015
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