Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. Web Development
  3. ASP.NET
  4. Viewstate Serialization Issue [modified]

Viewstate Serialization Issue [modified]

Scheduled Pinned Locked Moved ASP.NET
designsecurityxmljsonhelp
3 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • S Offline
    S Offline
    shecool
    wrote on last edited by
    #1

    Hi, I have a class marked as [Serializable] named "TestClass". When i store the object of this class Why the [NonSerialized()] attribute fname successfully serialized and deserialized back when i create the object from viewstate? Following is the full code of my test. using System; using System.Data; using System.Configuration; using System.Collections; using System.Web; using System.Web.Security; using System.Web.UI; using System.Web.UI.WebControls; using System.Web.UI.WebControls.WebParts; using System.Web.UI.HtmlControls; using System.Runtime.Serialization; using System.Xml.Serialization; public partial class viewstate : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) { TestClass obj = new TestClass(); obj.name = "Dreams"; obj.fname = "Lonely"; ViewState["MyDT"] = obj; } protected void Button1_Click(object sender, EventArgs e) { TestClass obj = (TestClass)ViewState["MyDT"]; Response.Write("id=" + obj.j.ToString() + " Name=" + obj.name.ToString() + " Age=" + obj.age.ToString() + " fname="+ obj.fname +""); } } [Serializable] public class TestClass { public int j = 1; public string name = "Dreams"; public string age = "28"; [NonSerialized()] public string fname = "Planet"; } Thanks

    G 1 Reply Last reply
    0
    • S shecool

      Hi, I have a class marked as [Serializable] named "TestClass". When i store the object of this class Why the [NonSerialized()] attribute fname successfully serialized and deserialized back when i create the object from viewstate? Following is the full code of my test. using System; using System.Data; using System.Configuration; using System.Collections; using System.Web; using System.Web.Security; using System.Web.UI; using System.Web.UI.WebControls; using System.Web.UI.WebControls.WebParts; using System.Web.UI.HtmlControls; using System.Runtime.Serialization; using System.Xml.Serialization; public partial class viewstate : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) { TestClass obj = new TestClass(); obj.name = "Dreams"; obj.fname = "Lonely"; ViewState["MyDT"] = obj; } protected void Button1_Click(object sender, EventArgs e) { TestClass obj = (TestClass)ViewState["MyDT"]; Response.Write("id=" + obj.j.ToString() + " Name=" + obj.name.ToString() + " Age=" + obj.age.ToString() + " fname="+ obj.fname +""); } } [Serializable] public class TestClass { public int j = 1; public string name = "Dreams"; public string age = "28"; [NonSerialized()] public string fname = "Planet"; } Thanks

      G Offline
      G Offline
      Gayani Devapriya
      wrote on last edited by
      #2

      Hi, First you need to Serialize the TestClass object. Here is how to do it. Serialization TestClass obj = new TestClass(); obj.name = "Dreams"; obj.fname = "Lonely"; FileStream fs = new FileStream("SerializedString.Data", FileMode.Create); BinaryFormatter bf = new BinaryFormatter(); bf.Serialize(fs, obj); fs.Close(); Then you need to deserialize it like this. Here is the code for that. Deserialization FileStream fs = new FileStream("SerializedString.Data", FileMode.Open); BinaryFormatter bf = new BinaryFormatter(); TestClass obj= (TestClass)bf.Deserialize(fs); fs.Close(); Response.Write("id=" + obj.j.ToString() + " Name=" + obj.name.ToString() + " Age=" + obj.age.ToString() + " fname="+ obj.fname +""); Hope it helped. Thx, Gayani

      S 1 Reply Last reply
      0
      • G Gayani Devapriya

        Hi, First you need to Serialize the TestClass object. Here is how to do it. Serialization TestClass obj = new TestClass(); obj.name = "Dreams"; obj.fname = "Lonely"; FileStream fs = new FileStream("SerializedString.Data", FileMode.Create); BinaryFormatter bf = new BinaryFormatter(); bf.Serialize(fs, obj); fs.Close(); Then you need to deserialize it like this. Here is the code for that. Deserialization FileStream fs = new FileStream("SerializedString.Data", FileMode.Open); BinaryFormatter bf = new BinaryFormatter(); TestClass obj= (TestClass)bf.Deserialize(fs); fs.Close(); Response.Write("id=" + obj.j.ToString() + " Name=" + obj.name.ToString() + " Age=" + obj.age.ToString() + " fname="+ obj.fname +""); Hope it helped. Thx, Gayani

        S Offline
        S Offline
        shecool
        wrote on last edited by
        #3

        I agreed. But storing object into the viewstate is also the process of serialization. If i removed the [Serializable] attribute from TestClass the application won't work, because to store the object, class must have that attribute. Than why that process won't understand [NonSerialized()] attribute? Is viewstate serialization is different form of process? Thanks,

        1 Reply Last reply
        0
        Reply
        • Reply as topic
        Log in to reply
        • Oldest to Newest
        • Newest to Oldest
        • Most Votes


        • Login

        • Don't have an account? Register

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • World
        • Users
        • Groups