Pushing a Geneic List of <T> to View state

Working on a DotNetNuke Project we had a requirement which was pretty.NET'ish and had nothing to do with DotNetNuke. All we wanted to do was to store a Generic list of in the view state - where was a XXX_Info class we usually write for DNN module.

For Any information to be retained in the view state it was obviously implied that it had to be serialized. If you're looking for some fairly complicated code to manually serialize Generic Classes you probably want to go here:

Here's another quick way if you just wanted to Save things to the view state:

  1. Mark the Class (and all parent classes if any) whose List you want to store in the viewstate as [Serializable].
  2. Convert the List to an Array.
  3. Store In View State.

Reconvert to List when reading from Viewstate.

Here's some quick sample code that illustrates:

   13     protected void Page_Load(object sender, EventArgs e)

   14     {

   15         // Quickly Illustrate  Creation of a List of that contains information;

   16         List<Customer> CustomerList = new List<Customer>;

   17         Customer SingleCustomer = new Customer;

   18         SingleCustomer.CustomerName = "foo";

   19         CustomerList.Add(SingleCustomer);

   20 

   21         // Convert the List to Array of Customers and Save To View State

   22         // Rather than Handling Serialization Manually

   23         ViewState.Add("Customers", CustomerList.ToArray);

   24 

   25         // While Reading the View State Information - Of course

   26         // use correct checks to see the item is Not Null and All that... and then do:

   27         Customer[] newArray = (Customer[])ViewState["Customers"];

   28 

   29         List<Customer> newList = new List<Customer>(newArray);

   30         Response.Write(CustomerList[0].CustomerName);

   31 

   32     }

   33 

   34     [Serializable]

   35     public class Customer

   36     {

   37         private string _CustomerName;

   38         public int CustomerName

   39         {

   40             get  { return _CustomerName;  }

   41             set  { _CustomerName = value; }

   42         }

   43     }

Pretty basic technique - which you definitely should NOT be using if the data volume in you collection item is Large - but works for scenarios where you want to save a small Generic List (3-10 odd items) to View State. Any other elegant ways of doing this?

This article is part of the GWB Archives. Original Author: Rajiv Popat

New on Geeks with Blogs

  • We Won The One Award I Actually Care About

    Full Scale made the Inc. 5000 for the fifth year straight, the 12th listing across my three companies. Here is why the one award you cannot buy is worth stopping for.

  • Your Customers Build the Features Now

    I let a tool I liked sit dead for a year rather than build the features I wanted. An MCP server meant I never had to, and your customers can do the same to your product.

  • Get the Size of a Directory in Linux the Easy Way

    du -sh for the quick answer, ncdu for the cleanup, df for the disk itself: every command for checking directory size in Linux, plus why du and df never agree.

  • Vim Search and Replace: The Ultimate Guide

    One :%s command replaces every match in a file before a find dialog would even open. The Vim substitute patterns worth the muscle memory: flags, ranges, capture groups, and multi-file edits.