Click here to Skip to main content
15,924,828 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I want to know that how to pass object of class in viewstate using serialization in asp dot net and to retrieve the information of class
Posted
Comments
TheKarateKid 18-Feb-15 13:01pm    
When one needs to maintain the object in viewstate, it has to be serializable, because it by default gets serialized to encrypted Base64 string. Please check the class which you are instantiating is a serializable class. Here is something to give you an idea
[Serializable]
public class Apple
{
public string Color { get;set;}
public decimal Weight { get;set;}
}


public Apple MyApple
{
get
{
if(ViewState["AppleColor"] !=null)
return (Apple)ViewState["AppleColor"];
else
return null;
}
set
{
ViewState["AppleColor"] = value;
}
}


Apple apple = new Apple();
apple.Color = "Red";
apple.Weight = 0.5m;


//set the property in viewstate
MyApple = apple;


Apple newApple = MyApple;

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900