本文介绍了如何序列化复杂的对象,并把它们放在一个ViewState的?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有类型的对象列表< SPSection> SPSection 是包含自定义类字符串列表< SPListItem> SPListItem 是SharePoint对象重新presenting一个项目。

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.

我要存储这个在的ViewState ,但我不知道如何做到这一点。有没有办法序列化此或将其转换为一些二进制字符串,这样我就可以把它放在一个的ViewState 。同样来自的ViewState ,我怎样才能将其转换回列表&LT获取值回来的时候; SPSection方式&gt;

I want to store this in a ViewState, but I don't know how to do this. Is there a way to serialize this or convert it to some binary string, so that I could put it in a ViewState. Also when getting the value back from the ViewState, how can I convert it back to List<SPSection>.

感谢

推荐答案

别这样做。 SPListItem 实例不能保留请求之间。它们取决于各自的 SPList 实例,而这又取决于的SPWeb + 的SPSite ,自动两种实例化,并通过 SPContext 。

Don't do this. SPListItem instances cannot be retained between requests. They depend on the respective SPList instance, which in turn depends on SPWeb + SPSite, both instantiated automatically and provided through SPContext.

你可以做的是保留项目 ID 取值请求之间。您的自定义类需要是二进制序列化,即标有 [Serializable接口] 接口。然后你只存储一个给定的键进入视图状态下,你的对象:的ViewState [MyObjects] = myObjects;

What you can do is retain item IDs between requests. Your custom classes need to be binary serializable, i.e. marked with the [Serializable] interface. You then just store your object under a given key into the view state: ViewState["MyObjects"] = myObjects;.

这篇关于如何序列化复杂的对象,并把它们放在一个ViewState的?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-29 01:08