Frequently Asked Interview Questions

Can I store any custom class object in a ViewState?

Yes, The class should be marked with [Serializable] attribute.
For Example:

[Serializable]
class Employee
{
int empId;
Public int EmployeeID
{
get { return empid; }
set {empid = value; }
}
}

//Create an object of Employee class

Employee empObj = new Employee();
empObj.EmployeeID = 1000;

//Store Employee class object in ViewState

ViewState["emp"] = empObj;

//Retrieve Employee class object from ViewState

Employee xyz = (Employee) ViewState["emp"];

Most Visited Pages

Home | Site Index | Contact Us