1

I am getting error in the following statement:

liPages = (List<ListItem>)(ViewState["CurrentSetOfPages"]);

The error i am getting is:

Type 'System.Web.UI.WebControls.ListItem' in Assembly 'System.Web, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a' is not marked as serializable.

But, when i use the following every thing seems to work perfect..

liPages = (List<ListItem>)(Session["CurrentSetOfPages"]);

Please clarify me on this..

1

3 Answers 3

2

since the result of storing items in ViewState is that those are stored in string on page (to maintain state between multiple requests), they need to be serializable, on the other hand Session is in memory storage (if you're using InProc mode, which is default, otherwise this doesn't apply), so objects don't need to be serializable.

Additionally see this answer for clarification: Why Viewstate can contain only serializable object?

Hope this helps.

1
  • The only time when objects don't need to be serializable for a session store is when using SessionStateMode.InProc. All other modes require objects to implement I serializable. Oct 3, 2013 at 13:17
1

Viewstate can contain only serializable objects whereas Session does not require serializable objects. That's why your code works for Session and not for viewstate.

Also, you can't store an object in viewstate unless it's marked as serializable.

Viewstate is always passed back to the browser as a serialized object. So, only serializable objects can be placed within it. If you do not put serializable objects in it, it cannot represent what it contains.

0

ViewState :

Serialization and Deserialization in ViewState is dependendent on ObjectStateFormatter and List (generic list) type is not supported by it. So you will be getting error.

To resolve this you need to handle your own serialization. You can use [Serializable] attibute to make it work. See this post in stackoverflow. In your case you need to create Custom Class that inherits from ListItem object and apply [Serializable] attribute.

Session: Please see this post in stackoverflow

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.