title: How to Serialize a ListItem Stored in Session
body: <p>I have this code which works fine on my local server:</p>
  
  <pre><code>if (Page.IsPostBack)
  {
      string[] questions = new string[20];
      string[] answers = new string[20];
      for (int i = 0; i &lt; lstQuestions.Items.Count; ++i)
      {
          questions[i] = lstQuestions.Items[i].Text;
          answers[i] = lstQuestions.Items[i].Value;
      }
      Session["questions"] = questions;
      Session["answers"] = answers;
      if (Session["qalist"] == null)
      {
          List&lt;ListItem&gt; qalist = lstQuestions.Items.Cast&lt;ListItem&gt;().ToList();
          Session["qalist"] = qalist;
      }
      else
      {
          List&lt;ListItem&gt; qalist = (List&lt;ListItem&gt;)Session["qalist"];
          lstQuestions.DataSource = qalist;
      }
  }
  
  else
  {
      if (Session["questions"] != null &amp;&amp; Session["answers"] != null &amp;&amp; Session["qalist"] != null)
      {
          List&lt;ListItem&gt; qalist = (List&lt;ListItem&gt;)Session["qalist"];
          foreach (ListItem item in qalist)
          {
              lstQuestions.Items.Add(new ListItem(item.Text, item.Value.ToString()));
          }
      }
  }
  </code></pre>
  
  <p>However, when I go to the web server, I get the error "System.Web.UI.WebControls.ListItem, not marked as serializable". I've looked into how I can make this serializable, or even how to convert "qalist" to a value that is in fact serializable, and then save that value to a session. However, my efforts were to no avail. Would serializing be the best route? Finding a different way to store this data? Or something else?</p>