[英]How to serialize complex objects and put them in a ViewState?
I have an object of type List<SPSection>
, the SPSection
is a custom class that contains a string
and a List<SPListItem>
, SPListItem
is a SharePoint object representing an item. 我有一个类型为
List<SPSection>
的对象,该SPSection
是一个自定义类,其中包含一个string
和一个List<SPListItem>
, SPListItem
是一个代表项目的SharePoint对象。
I want to store this in a ViewState
, but I don't know how to do this. 我想将其存储在
ViewState
,但是我不知道该怎么做。 Is there a way to serialize this or convert it to some binary string, so that I could put it in a ViewState
. 有没有一种方法可以将此序列化或将其转换为一些二进制字符串,以便可以将其放在
ViewState
。 Also when getting the value back from the ViewState
, how can I convert it back to List<SPSection>
. 另外,当从
ViewState
取回值时,如何将其转换回List<SPSection>
。
Thanks 谢谢
Don't do this. 不要这样
SPListItem
instances cannot be retained between requests . SPListItem
实例不能在请求之间保留 。 They depend on the respective SPList
instance, which in turn depends on SPWeb
+ SPSite
, both instantiated automatically and provided through SPContext
. 它们依赖于各自的
SPList
实例,而后者又依赖于SPWeb
+ SPSite
,它们都自动实例化并通过SPContext
提供。
What you can do is retain item ID
s between requests. 您可以做的是在请求之间保留商品
ID
。 Your custom classes need to be binary serializable, ie marked with the [Serializable]
interface. 您的自定义类需要是二进制可序列化的,即用
[Serializable]
接口标记。 You then just store your object under a given key into the view state: ViewState["MyObjects"] = myObjects;
然后,您只需在给定键下将对象存储到视图状态即可:
ViewState["MyObjects"] = myObjects;
. 。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.