Can we hold object in ViewState
Can we assign object of any class in ViewState variable in Asp.Net application ?
3 Answers
Know the answer? Post it. Somebody with the same question will find it here.
Know the answer? Post it. Somebody with the same question will find it here.
Answer a question in the forums and earn +20 SHARP for every answer.
See unanswered questions
DevSocialUpcoming · 3rd editionAI Agents ConferenceDec 1–3, 2026Live onlineFree to attend5days leftRegister
Posted Jun 24, 2012, 7:54 PM EDT
public class Student
{
public string firstName;
public string lastName;
public Student(string fName, string lName)
{
firstName = fName;
lastName = lName;
}
}
Because the Student class is marked as serializable, it can be stored in view state:
// Storing a student in view state.
Student stud = new Student("John", "Doe");
ViewState["CurrentStudent"] = stud;
Remember, when using custom objects, you'll need to cast your data when you retrieve it from view state.
// Retrieve a student from view state.
Student stud = (Student) ViewState["CurrentStudent"];
http://msdn.microsoft.com/en-us/library/ms227551(v=vs.85).aspx
http://www.beansoftware.com/ASP.NET-Tutorials/ViewState-In-ASP.NET.aspx
Posted Jun 22, 2012, 3:42 AM EDT
[XMLSerializable]
public class myclass
{
///class code goes here
}
Shubham SrivastavaRank #290 · 8.7K reputationPosted Jun 24, 2012, 7:23 AM EDT