Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Which types of objects can we place in view state?

I want to know why we must set the serializable attribute to save an object in view state.

Also, which type of objects can we store in view state?

like image 801
Nishant Kumar Avatar asked Aug 27 '10 11:08

Nishant Kumar


People also ask

Which of the following object can be stored in ViewState?

ViewState is a serialized collection of objects so any serializable objects may be put in there. Save this answer.

What is ViewState explain with example?

View State is the method to preserve the Value of the Page and Controls between round trips. It is a Page-Level State Management technique. View State is turned on by default and normally serializes the data in every control on the page regardless of whether it is actually used during a post-back.

Can we store dataTable in ViewState?

cs code behind file and write the following code to create and save the datatable into viewstate and bind the GridView as: private void AddDefaultFirstRecord() { //creating dataTable.

Can we use ViewState in MVC?

ASP.NET MVC does not use ViewState in the traditional sense (that of storing the values of controls in the web page). Rather, the values of the controls are posted to a controller method.


1 Answers

ViewState is serialized using binary serialization using ObjectStateFormatter. Quote from the documentation:

The ObjectStateFormatter class is optimized to serialize and format many common .NET Framework reference types, as well as constants. The following table lists the types that are optimized.

Array, DateTime, Int16, String, ArrayList, Double, Int32, String [], Boolean, Enum, null (Nothing), String.Empty, Byte, Hashtable, Pair, Triplet, Char, HybridDictionary, Single, Type, Color, IDictionary,

Additionally, while conventional string types and string arrays are written to and from a serialized binary writer unaltered, some strings are optimized by creating internal string tables. Strings are optimized using these tables if the string has an associated TypeConverter object or if the string is actually an instance of the IndexedString class.

Other types not listed above are binary-serialized using a BinaryFormatter object if they implement the ISerializable interface or are decorated with the SerializableAttribute attribute. The ObjectStateFormatter class is not optimized for any of these serializable types.

If the ObjectStateFormatter class encounters a type that is not serializable, an ArgumentException exception is thrown.

For an object to be binary serializable in the ViewState it needs to be decorated with the [Serializable] attribute. So you can put in ViewState any object that has this attribute. Note that simple types like string, int, float, ... can also be placed in ViewState.

like image 99
Darin Dimitrov Avatar answered Sep 20 '22 19:09

Darin Dimitrov