how to store anonymous object in view state and fetch them.
For Example ViewState["demo"]=new{id=10,Name="DemoUser"};
and to store it in two variable like int Id,string Name.
Friends i have searched a lot no one told how to convert from ViewState to anonymous object.
Please Help me Friends.
Regards
Vimal Raj Kumar
You're going to have to handle your own serialisation and de-serialisation. However it will require you to at least know something about the object structure which makes the anonymnous type a bit useless, you might as well define a concrete type for it
if (!Page.IsPostBack)
{
var myObject = new { id = 10, Name = "DemoUser" };
JavaScriptSerializer s = new JavaScriptSerializer();
string data = s.Serialize(myObject);
ViewState["demo"] = data;
}
else
{
string data = (string)ViewState["demo"];
JavaScriptSerializer s = new JavaScriptSerializer();
dynamic dynObject = s.DeserializeObject(data); // you can use Dictionaryinstead of dynamic
var myObject = new { id = dynObject["id"], Name = dynObject["Name"] };
}
Edit: JavascriptSerializer is in System.Web.Script.Serialization
沒有留言:
張貼留言