1
votes

I have added one datalist into view state as:

ViewState["datalist"] = dtlstForm;

and retrieved it as:

DataList lis = (DataList)ViewState["datalist"];

then folowing error comes:

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

2
The question to ask is "why" do you want to put this control into the ViewState? What are you trying to achieve by doing this?Robin Day
Can I ask why you're putting a DataList into ViewState? DataList is a control, and there is a perfectly good Controls hierarchy in ASP .NET.Paul Alan Taylor

2 Answers

3
votes

The DataList class is not serializable (the SerializableAttibute has not be set on it and it is not implementing the ISerializable interface).

This mean the .NET framework cannot serialize it and put it into ViewState.

Since this is a built in class, you cannot modify it to be serializable.

As a DataList is expected to hold quite a lot of information, putting it in ViewState would cause ViewState to be huge, which would impact performance, so it makes sense to not make it serializable.

Perhaps you can rethink the information you need to put in ViewState and only put a small amount into it (a list of IDs for example).

1
votes

You can't put an object in the viewstate unless it implements ISerializable. The viewstate is serialised before it is sent to the client.

You can use Session in a similar way to viewstate and for all intents and purposes it will be ok. Session I would presume is more resource hungry.

Is datalist your own class? If so you could implement ISerializable as well but I wouldn't go down that route if I could just type session instead.