0

Say for example if I have a business entity -> Customer, which has customerId, customerName and customerType. I have created an asp:Hidden Variable hdnCustomer to runat="server"

If I wanted to serialize the value of the customer business entity (in the code behind) to the hdnCustomer then how would I do that? Also once serialized how would I deserialize it?

// Pseudo code

Collection<Customer> customerList = new Collection<Customer>();

customerList = BusinessAccess.GetCustomerList();

hdnCustomer = serialize and assign the value of 'customerList' to hdnCustomer;

...

...

// Later on a select index change of one of the drop down lists

Inside the event handler for the drop down list:

{

Collection<Customer> customerList = new Collection<Customer>();

customerList = deserialize the value from hdnCustomer

int a = Convert.ToInt32(ddlDropDown.SelectedValue);

foreach(a in customerList)

{

// Do something

}

}
Sicco
  • 6,167
  • 5
  • 45
  • 61
WalkMan
  • 3
  • 1

2 Answers2

0

I think .net has already providing some classes to do so, look at this example

Prashant Lakhlani
  • 5,758
  • 5
  • 25
  • 39
0

You can serialise to and from XML using XmlSerializer:

http://support.microsoft.com/kb/815813

However, if you just store the object in the ViewState[] collection that should work better:

ViewState["Customer"] = customerList;

It does the same thing: store the serialisable object in the page, hidden from the user: but it won't be in a human-readable format.

(edit: To deserialise, just get the value of ViewState["Customer"], checking for a null before using it!)

edit 2: a useful link about storing objects in ViewState:

http://www.beansoftware.com/ASP.NET-Tutorials/ViewState-In-ASP.NET.aspx

Hope that helps.

Kieren Johnstone
  • 41,277
  • 16
  • 94
  • 144