如何坚持名单< T>在ASP.NET自定义控件的财产? [英] How to persist List<T> property in ASP.NET Custom Control?

查看:90
本文介绍了如何坚持名单< T>在ASP.NET自定义控件的财产?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个自定义的控制以下属性:

I have the following property in a custom control:

List<myClass> _items;
public List<myClass> Items{
    get { return _items; }
    set { _items= value; }
}

在我的codebehind,我将项目添加到集合中...

In my codebehind, I add items to the collection as in...

myCustomControl.items.Add(new myClass());

不过,这些都不是在回传仍然存在。什么是正确的方式,允许在自定义控件持久性?

However, these are not persisted across postbacks. What is the proper way to allow persistance in custom controls?

推荐答案

哎呀!不要把一个List&LT;>到的ViewState!这将是巨大的!

Yikes! Don't put a List<> into the ViewState! It'll be massive!

如果您添加一个List&LT;串GT;它包含两个元素 - ABC和XYZ到ViewState中,它会由312个字节长

If you add a List<string> that contains two elements - "abc" and "xyz" into the ViewState, it'll grow by 312 bytes.

如果相反,你添加一个字符串[]包含相同的两个元素,它会只有24字节长。

If instead you add a string[] that contains the same two elements, it'll only grow by 24 bytes.

而这只是字符串列表!你可以把你的类在那里,科里·唐尼所暗示的,但你的ViewState将蘑菇!

And that's just lists of strings! You can put your classes in there as Corey Downie suggests, but your ViewState will mushroom!

要保持一个合理的大小,你就必须去一些努力,您的项目列表转换为字符串数组,然后再返回。

To keep it a sensible size, you'll have to go to some effort to convert your list of items into arrays of strings and back again.

作为替代方案,考虑把你的对象到会话中。这样,你的对象将被存储在服务器上,而不是被序列化到ViewState中,并发送到浏览器和背部。

As an alternative, consider putting your objects into the Session: that way your objects will be stored on the server, instead of being serialized into the ViewState and sent to the browser and back.

这篇关于如何坚持名单&LT; T&GT;在ASP.NET自定义控件的财产?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆