存储字典<字符串,字符串>在ASP.NET视图状态? [英] Storing Dictionary<string, string> in ASP.NET view state?

查看:104
本文介绍了存储字典<字符串,字符串>在ASP.NET视图状态?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图存储词典<字符串,字符串> 在自定义控制的ViewState我开发ASP.NET 2.0:

I'm trying to store a Dictionary<string, string> in the ViewState of a custom control I'm developing for ASP.NET 2.0:

private Dictionary<String, String> Items
    {
        get
        {
            object d = ViewState["Items"];
            return (d == null ? null : (Dictionary<String, String>)d);
        }
        set
        {
            ViewState["Items"] = value;
        }
    }

访问它看起来是这样的:

Accessing it looks like this:

public void UpdateData
{
    if (this.Items == null)
        this.Items = new Dictionary<string, string>();
    else
        this.Items.Clear();
    //Fill the collection
}

当它被设置在第一时间加载页面,这似乎很好地工作。但在随后的回传,返回的值始终为空(第一个条件总是会发生的)。调试表明,它是让出的ViewState在酒店 GET

When it gets set the first time the page loads, it appears to work fine. But on subsequent postbacks, the value returned is always null (the first condition always happens). Debugging shows that it's getting null out of the ViewState in the property get.

我做了一些研究,并发现的类必须实现 IStateManager 是在ViewState中saveable和的词典MSDN页面似乎表明词典&LT; TKEY的,TValue&GT; 不。但我在ViewState中之前存储的字典没有问题。这里发生了什么?是我的previous经验侥幸?

I've done some research and have found that classes must implement IStateManager to be saveable in ViewState, and the Dictionary MSDN page appears to indicate that Dictionary<TKey, TValue> does not. But I've stored dictionaries before in ViewState without a problem. What's going on here? Was my previous experience a fluke?

更新:我尝试添加一些测试code到属性:的ViewState [ItemTest] =富; 设置字符串测试=(字符串)的ViewState [ItemTest]; GET 。像词典,它出来空。所以它不会出现要与词典可序列化的问题。此外,为了澄清,的UpdateData 从我 RenderControl 覆盖,这在的Page_Load <发生所谓的/ code>中包含该控件的页面。

UPDATE: I tried adding some test code to the property: ViewState["ItemTest"] = "foo"; in the set and string test = (string)ViewState["ItemTest"]; in the get. Like the Dictionary, it comes out null. So it doesn't appear to be a problem with the Dictionary being serializable. Also, to clarify, UpdateData is called from my RenderControl override, which happens after Page_Load in the page that contains the control.

推荐答案

您可以存储在ViewState中的字典里,但你正试图做到这一点在页面生命周期为时已晚。正如ViewState是初始化加载后,ViewState是保存的的控件呈现。将你的逻辑出 RenderControl ,进入较早的生命周期,如 preRender 。

You can store the dictionary in ViewState, but you are attempting to do this too late in the page life cycle. Just as ViewState is loaded after Init, ViewState is saved before controls are rendered. Move your logic out of RenderControl and into another method or event handler earlier in the life cycle, such as PreRender.

protected override void OnPreRender(EventArgs e)
{
    if (this.Items == null)
    {
        this.Items = new Dictionary<string, string>();
    }

    base.OnPreRender(e);
}

您会注意到该对象不再是空的,只要后续的回传为ViewState是没有在任何控制或其父禁用。

You will notice the that object is no longer null on subsequent postbacks as long as ViewState is not being disabled on either the control or its parent.

这篇关于存储字典&LT;字符串,字符串&GT;在ASP.NET视图状态?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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