We can store objects in viewstate like we store string or integer. But before storing we need to convert them into stream of bytes to keep tem in hidden field. So we need to use Serialization. And object which can’t be serialised, they will not be able to keep in viewstate.
[Serializable] public class Student { public int Roll; public string Name; public void AddStudent(int intRoll, string strName) { this.Roll = intRoll; this.Name = strName; } }
Now we need to store them to viewstate.
Student _objStudent = new Student(); _objStudent.AddStudent(2, "Max"); ViewState["StudentObj"] = _objStudent; //Retrieving student Student _objStudent; _objStudent = (Student)ViewState["StudentObj"];