简体   繁体   中英

How to fix serialize for table in Viewstate?

I've declared a viewstate in my page like this:

public class TMP_RequestCourse
{
    public int CourseCode;
    public string CourseTitle;
    public int PriorityID;
}
public TMP_RequestCourse T_RequestCourse
{
    get
    {
        if (ViewState["TMP_RequestCourse"] == null)
            return new TMP_RequestCourse();
        return (TMP_RequestCourse)ViewState["insertMode"];
    }
    set { ViewState["TMP_RequestCourse"] = value; }
}

but when page loaded, I recieve following error:

Type 'App.UI.Pages.EduRequestEdit+TMP_RequestCourse' in Assembly 'App.UI, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null' is not marked as serializable.

You should add SerializableAttribute to your class.

[Serializable]
public class TMP_RequestCourse
{
    public int CourseCode;
    public string CourseTitle;
    public int PriorityID;
}

When you create an object in a .Net framework application, you don't need to think about how the data is stored in memory. Because .Net framework takes care of that for you. However, if you want to store the contents of an object to a file, send an object to another process or transmit it across the network, you do have to think about how the object is represented because you will need to convert it to a different format. This conversion is called SERIALIZATION.

Reference

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM