views:

12

answers:

2

Question. 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 can u please show how i would deserialize it.

// Psudo code

Collection customerList = new Collection();

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 customerList = new Collection();

customerList = deserialize the value from hdnCustomer

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

foreach(a in customerList)

{

// Do something

}

}

Thanks

A: 

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

lakhlaniprashant.blogspot.com
A: 

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