2010-06-26 29 views
0

例如说如果我有一个业务实体 - >客户,它具有customerId,customerNamecustomerType。我创建了一个asp:隐藏变量hdnCustomer runat =“服务器”将业务实体分配给隐藏变量

如果我想将客户业务实体的值(在后面的代码中)序列化到hdnCustomer,那么我该如何做?还有一次序列化,我将如何反序列化它?

// 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 

内的下拉列表中选择事件处理程序:

{ 

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

customerList = deserialize the value from hdnCustomer 

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

foreach(a in customerList) 

{ 

// Do something 

} 

} 

回答

0

您可以使用序列化和XML XmlSerializer:

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

但是,如果你只是存储对象的的ViewState []集合中应该更好的工作:

ViewState["Customer"] = customerList; 

它做同样的事情:存储在页面中serialisable对象,对用户隐藏:但它不会以人类可读的格式。

(编辑:要deserialise,刚刚获得ViewState中的值“客户”],检查空在使用它之前!)

编辑2:

:大约在ViewState中存储对象的有用链接

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

希望有所帮助。