如何在ASP.NET自定义控件中持久保存List 属性?

时间:2022-09-02 13:29:14

I have the following property in a custom control:

我在自定义控件中有以下属性:

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

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?

但是,这些不会在回发中持续存在。在自定义控件中允许持久性的正确方法是什么?

5 个解决方案

#1


6  

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

哎呀!不要将List <>放入ViewState!这将是巨大的!

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

如果将包含两个元素的List - “abc”和“xyz”添加到ViewState中,它将增长312个字节。

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

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

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

这就是字符串列表!你可以把你的课程放在那里,正如Corey Downie建议的那样,但是你的ViewState会变成蘑菇!

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.

为了保持合理的大小,你必须付出一些努力将你的项目列表转换为字符串数组并再次返回。

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.

作为替代方案,请考虑将对象放入Session中:这样您的对象将存储在服务器上,而不是被序列化到ViewState中并发送到浏览器并返回。

#2


2  

One way to overcome the size problem with a generic list is to persist it in ViewState as its basic array type:

使用泛型列表克服大小问题的一种方法是将其作为基本数组类型保存在ViewState中:

protected List<string> Items 
{ 
    get 
    { 
        if (ViewState["Items"] == null)
            ViewState["Items"] = new string[0];
        return new List<string>((string[])ViewState["Items"]);
    }
    set
    {
        ViewState["Items"] = value.ToArray();
    }
}

#3


1  

if you are talking about persisting the data across postbacks of the same page then you can manually add the items to the ViewState and the retrieve them On Load.

如果您正在讨论在同一页面的回发之间保留数据,那么您可以手动将项目添加到ViewState并在加载时检索它们。

#4


1  

You can store them in the controls viewstate

您可以将它们存储在控件视图状态中

public List<myClass> Items{
    get { return this.ViewState["itemsKey"] }
    set { this.ViewState["itemsKey"]= value; }
}

#5


1  

I agree that there are issues with a List<> in viewstate but it does work. Note that there is no setter on this by design. You need a reference to the list object object and your get method can new up a list when needed.

我同意在viewstate中存在List <>的问题,但它确实有效。请注意,此设计没有设置器。您需要对列表对象对象的引用,并且您的get方法可以在需要时新建列表。

protected List<myClass> Items
{
    get
    {
        if (ViewState["myClass"] == null)
            ViewState["myClass"] = new List<myClass>();

        return (List<myClass>)ViewState["myClass"];
    }
}

#1


6  

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

哎呀!不要将List <>放入ViewState!这将是巨大的!

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

如果将包含两个元素的List - “abc”和“xyz”添加到ViewState中,它将增长312个字节。

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

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

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

这就是字符串列表!你可以把你的课程放在那里,正如Corey Downie建议的那样,但是你的ViewState会变成蘑菇!

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.

为了保持合理的大小,你必须付出一些努力将你的项目列表转换为字符串数组并再次返回。

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.

作为替代方案,请考虑将对象放入Session中:这样您的对象将存储在服务器上,而不是被序列化到ViewState中并发送到浏览器并返回。

#2


2  

One way to overcome the size problem with a generic list is to persist it in ViewState as its basic array type:

使用泛型列表克服大小问题的一种方法是将其作为基本数组类型保存在ViewState中:

protected List<string> Items 
{ 
    get 
    { 
        if (ViewState["Items"] == null)
            ViewState["Items"] = new string[0];
        return new List<string>((string[])ViewState["Items"]);
    }
    set
    {
        ViewState["Items"] = value.ToArray();
    }
}

#3


1  

if you are talking about persisting the data across postbacks of the same page then you can manually add the items to the ViewState and the retrieve them On Load.

如果您正在讨论在同一页面的回发之间保留数据,那么您可以手动将项目添加到ViewState并在加载时检索它们。

#4


1  

You can store them in the controls viewstate

您可以将它们存储在控件视图状态中

public List<myClass> Items{
    get { return this.ViewState["itemsKey"] }
    set { this.ViewState["itemsKey"]= value; }
}

#5


1  

I agree that there are issues with a List<> in viewstate but it does work. Note that there is no setter on this by design. You need a reference to the list object object and your get method can new up a list when needed.

我同意在viewstate中存在List <>的问题,但它确实有效。请注意,此设计没有设置器。您需要对列表对象对象的引用,并且您的get方法可以在需要时新建列表。

protected List<myClass> Items
{
    get
    {
        if (ViewState["myClass"] == null)
            ViewState["myClass"] = new List<myClass>();

        return (List<myClass>)ViewState["myClass"];
    }
}