Problem storing a Generic list object in Viewstate

I had an generic object that implements and I was just trying to store that object in ViewState. THis was where i first stumbled as it was giving me an error that basically tells that “Cannot serialze a generic list object”. After I did a bit of googling I found out that I need to make that object XmlSerializable inoder to be strored in Viewstate.

I used the [serializable ] attribute on the class that I wanted to serialize.

Still i was giving me error. After a quite prolonged step into this problem my senior (read Project Manager) found out a nice cool solution.

I addition to that attribute what he did is as follows.

 

public partial class _Default : System.Web.UI.Page

{

    protected void Page_Load(object sender, EventArgs e)

    {

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

        List<Customer> CustomerList = new List<Customer>();

        Customer SingleCustomer = new Customer();

        SingleCustomer.CustomerAddress = "my address";

        SingleCustomer.CustomerName = "my name";

        CustomerList.Add(SingleCustomer);

 

        // Convert the List to Array and Save To View State

        // Rather than Handling Serialization Manually

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

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

 

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

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

 

    }

 

    [Serializable]

    public class Customer

    {

        private string _CustomerName = "";

        private string _CustomerAddress = "";

 

        public string CustomerName

        {

            get

            {

                return _CustomerName;

            }

            set

            {

                _CustomerName = value;

            }

       

        }

        public string CustomerAddress

        {

            get

            {

                return _CustomerAddress;

            }

            set

            {

                _CustomerAddress = value;

            }

        }

 

 

    }

}

 

  • Share This Post:
  • Share on Twitter
  • Share on Facebook
  • Share on Technorati
Print | posted on Tuesday, June 13, 2006 7:22 AM

Feedback

# re: Problem storing a Generic list object in Viewstate

left by Peter Widmer at 8/11/2006 5:46 PM Gravatar
Thanks alot man!!! That was 1:1 what I've been looking for :) Thousand thanks

# re: Problem storing a Generic list object in Viewstate

left by Dheeman Dutta at 8/15/2006 5:41 PM Gravatar
Thanks Peter For dropping a line, :)

# Just saved a lot of work

left by Andreas at 4/19/2007 12:19 AM Gravatar
Thanks, just what I was looking for.

Andreas

# re: Problem storing a Generic list object in Viewstate

left by Dheeman at 4/19/2007 2:20 AM Gravatar
Thanks Andreas for dropping a line.

# re: Problem storing a Generic list object in Viewstate

left by Reji at 6/6/2007 3:02 AM Gravatar
Good yaarr.... Thanks for Posting cool stuff..

# re: Problem storing a Generic list object in Viewstate

left by Newbie at 9/26/2007 9:48 AM Gravatar
Thanks for the posting!

One of the issues I have run into is with dynamically creating an HtmlTable and storing controls within the TableRow. The problem with HtmlTable is the viewState of the TableRow is not saved therefore the controls are lost during Postbacks. Currently I store the TableRow in a Session and then rebuild the HtmlTable on the PostBack.

How can you store the TableRow in a ViewState? Do you have to create a custom class that extends the TableRow just to make it serializable so it can be stored in the ViewState or are there other alternatives?

# re: Problem storing a Generic list object in Viewstate

left by Dheeman Dutta at 9/26/2007 4:14 PM Gravatar
You can refer this post from
http://geekswithblogs.net/opiesblog/archive/2006/05/22/79270.aspx

anyways , thanks for dropping a line

# re: Problem storing a Generic list object in Viewstate

left by Adam at 11/8/2007 9:37 AM Gravatar
I was about to give up on trying to get this to work. Your solution works great....thanks!

# re: Problem storing a Generic list object in Viewstate

left by Chandrasekhar at 4/12/2009 8:05 PM Gravatar
Your posting is really great

# re: Problem storing a Generic list object in Viewstate

left by Celal Ak at 6/6/2009 10:55 PM Gravatar
Thanks for the posting!Your posting is really great.

# re: Problem storing a Generic list object in Viewstate

left by Deep Singh Chauhan at 9/2/2010 10:39 AM Gravatar
Thanks buddy, I was stuck on the same kind of problem. Now got the solution in your post. thanks for sharing.

# re: Problem storing a Generic list object in Viewstate

left by gcoleman0828 at 1/14/2011 11:57 PM Gravatar
To anyone who is doing this sort of thing now (2010) .net 4.0+ ViewState is fine with a list now and you can easily just add the list and reference it in your code

ViewState.Add("MasterExtList", _extList);
ExtList newList = new ExtList();
newList = (ExtList)ViewState["MasterExtList"];

NotificationLabel.Text = newList.ExtItemList[0].Name;

# re: Problem storing a Generic list object in Viewstate

left by anantha rengan at 6/14/2011 8:13 PM Gravatar
Thanks
Post A Comment
Title:
Name:
Email:
Website:
Comment:
Verification: