Dheeman Dutta

Just Another Blog.....

  Home  |   Contact  |   Syndication    |   Login
  39 Posts | 1 Stories | 29 Comments | 12 Trackbacks

News



Archives

Post Categories

.NET Links

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;

            }

        }

 

 

    }

}

 

posted on Tuesday, June 13, 2006 7:22 AM

Feedback

# re: Problem storing a Generic list object in Viewstate 8/11/2006 5:46 PM Peter Widmer
Thanks alot man!!! That was 1:1 what I've been looking for :) Thousand thanks

# re: Problem storing a Generic list object in Viewstate 8/15/2006 5:41 PM Dheeman Dutta
Thanks Peter For dropping a line, :)

# Just saved a lot of work 4/19/2007 12:19 AM Andreas
Thanks, just what I was looking for.

Andreas

# re: Problem storing a Generic list object in Viewstate 4/19/2007 2:20 AM Dheeman
Thanks Andreas for dropping a line.

# re: Problem storing a Generic list object in Viewstate 6/6/2007 3:02 AM Reji
Good yaarr.... Thanks for Posting cool stuff..

# re: Problem storing a Generic list object in Viewstate 9/26/2007 9:48 AM Newbie
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 9/26/2007 4:14 PM Dheeman Dutta
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 11/8/2007 9:37 AM Adam
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 4/12/2009 8:05 PM Chandrasekhar
Your posting is really great

# re: Problem storing a Generic list object in Viewstate 6/6/2009 10:55 PM Celal Ak
Thanks for the posting!Your posting is really great.

Post A Comment
Title:
Name:
Email:
Website:
Comment:
Verification: