Johnathan Nardia
Johnathan Nardia

Reputation: 71

ViewState list of objects c#

hallo iam getting this error message when the page is load,

Type 'messageBox' in Assembly 'App_Code.e9hyffkh, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null' is not marked as serializable.

iam new in asp.net , and i can find what iam going worng!

heres my code:

public partial class home : System.Web.UI.Page
{
public string val = "";
public string data = "";

protected void Page_Load(object sender, EventArgs e)
{
    val = "";
    if (!IsPostBack)
    {
        ViewState["Messages"] = new List<messageBox>();
    }

}
protected void ListBox1_SelectedIndexChanged(object sender, EventArgs e)
{

}
protected void Button1_Click(object sender, EventArgs e)
{

    var messages = (List<messageBox>)ViewState["Messages"];

    if (text1.Text == "")
    {
        val = "נא הכנס שם";
    }
    else
    {
        messages.Add(text1.Text);
        val = "נוסף בהצלחה";
    }

    ListBox1.DataSource = messages;
    ListBox1.DataBind();
    ViewState["Messages"] = messages;
    data = messages.Count.ToString();

    text1.Text = "";


}

Upvotes: 1

Views: 6586

Answers (2)

MethodMan
MethodMan

Reputation: 18843

Add [Serializable] attrubute to your class not sure what your class looks like but see below for an example

[Serializable]
public class messageBox
{
   //other code / Fields related to the class goes below
}

Upvotes: 2

Jakub Konecki
Jakub Konecki

Reputation: 46008

Add [Serializable] attribute on top of your messageBox class.

For reference: SerializableAttribute Class

Upvotes: 4

Related Questions