Click here to Skip to main content
Rate this: bad
good
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");
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 30-Jan-13 4:38am

1 solution

Rate this: bad
good
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 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[^]
  Permalink  
Comments
Espen Harlinn at 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
0 OriginalGriff 432
1 Kornfeld Eliyahu Peter 420
2 Yogesh Kumar Tyagi 244
3 Nelek 115
4 Gihan Liyanage 99
0 Kornfeld Eliyahu Peter 420
1 OriginalGriff 303
2 Yogesh Kumar Tyagi 230
3 Gihan Liyanage 99
4 sankarsan parida 90


Advertise | Privacy | Mobile
Web01 | 2.8.140827.1 | Last Updated 30 Jan 2013
Copyright © CodeProject, 1999-2014
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