存储字典在 ASP.NET 视图状态下?
我正在尝试在我为 ASP.NET 2.0 开发的自定义控件的 ViewState 中存储 Dictionary
:
private Dictionary<String, String> Items
{
get
{
object d = ViewState["Items"];
return (d == null ? null : (Dictionary<String, String>)d);
}
set
{
ViewState["Items"] = value;
}
}
访问它看起来像这样:
public void UpdateData
{
if (this.Items == null)
this.Items = new Dictionary<string, string>();
else
this.Items.Clear();
//Fill the collection
}
第一次设置时页面加载,看起来工作正常。但在随后的回发中,返回的值始终为 null(第一个条件总是发生)。调试显示,它从属性 get
中的 ViewState 中获取 null
。
我做了一些研究并发现该类必须实现 IStateManager 才能保存在 ViewState 中,并且 字典 MSDN 页面 似乎表明 Dictionary
没有。但我之前在 ViewState 中存储字典没有问题。这是怎么回事?我之前的经历是侥幸吗?
更新:我尝试向属性添加一些测试代码: set
中的 ViewState["ItemTest"] = "foo";
和 string test = (string)
。与 get
中的 ViewState["ItemTest"];Dictionary
一样,它的结果为 null。因此,Dictionary
的可序列化似乎不存在问题。另外,需要澄清的是,UpdateData
是从我的 RenderControl
覆盖中调用的,该覆盖发生在包含控件的页面中的 Page_Load
之后。
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
}
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
.
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?
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可以将字典存储在 ViewState 中,但您尝试在页面生命周期中执行此操作为时已晚。正如 ViewState 在
Init
之后加载一样,ViewState 在控件呈现之前保存。将您的逻辑从RenderControl
移出并移至生命周期早期的另一个方法或事件处理程序中,例如PreRender
。您将注意到,只要 ViewState 未在控件或其父级上禁用,该对象在后续回发中就不再为 null。
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 ofRenderControl
and into another method or event handler earlier in the life cycle, such asPreRender
.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.
确保您没有在页面生命周期中过早访问该属性,因为视图状态不是直接加载的。
您第一次从什么方法(在页面生命周期的哪个点)请求此属性?
Make sure you're not accessing the property too early in the page lifecycle as the viewstate is not loaded directly.
From what method (at which point in the page lifecycle) are you requesting this property for the first time?