简体   繁体   English

如何序列化复杂的对象并将其放入ViewState?

[英]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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM