OrderedDictionary 是只读的,无法修改
问:
当我尝试以下代码时:
((IOrderedDictionary)Session["keys"])[2] = objToUpdate.Note_title;
((IOrderedDictionary)Session["keys"])[3] = objToUpdate.Iscourse;
它抛出异常:
OrderedDictionary 是只读的并且 无法修改。
我的问题:
- 为什么只是只读?
- 如何在会话中设置我的密钥,解决我的问题?
编辑:
我通过以下方式填充IOrderedDictionary
:
IOrderedDictionary keys = gv_courses.DataKeys[index].Values;
Session["Keys"] = keys;
Q:
When i try the following code :
((IOrderedDictionary)Session["keys"])[2] = objToUpdate.Note_title;
((IOrderedDictionary)Session["keys"])[3] = objToUpdate.Iscourse;
It throws an exception:
The OrderedDictionary is read only and
cannot be modified.
My question:
- Why just read only?
- How to set my key in the session,fixing my problem?
EDIT:
I fill the IOrderedDictionary
through this:
IOrderedDictionary keys = gv_courses.DataKeys[index].Values;
Session["Keys"] = keys;
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
强力解决方案是在保存到会话之前将值复制到新字典:
但是,我强烈建议找到另一种方法来获取密钥,而不使用 GridView 的 DataKeys 属性。
Brute force solution is to copy values to new dictionary before saving to session:
However, I strongly recommend to find another way to get your keys without using DataKeys property of your GridView.
该代码给出只读错误,因为 Session["Keys"] 中的引用指向只读的 gv_courses.DataKeys[index].Values 。整个 DataKeyArray Datakeys 以及每个 DataKey 元素的 Values 属性都是只读的。从我在互联网上读到的内容来看,这是一个 .net 设计选择,它可能是只读的,因此它与数据源保持同步(如果您可以修改 DataKeyArray,它必须传播到数据源)。
您需要做的是制作 gv_courses.DataKeys[index].Values 的深层副本,然后将该新副本保存在 Session["keys"] 中。
例如:
键 OrderedDictionary 与 gv_courses.DataKeys[index].Values 相同,但它不是只读的。现在您可以将其保存到会话中:
现在代码将运行,因为会话中的 OrderedDictionary 不再是只读的:
The code gives the read only error because the reference in Session["Keys"] point to gv_courses.DataKeys[index].Values which is read only. The entire DataKeyArray Datakeys is read only and also the Values property of each of his DataKey element. From what i read on the internet this is a .net design choice and it is probably read only so it stays in sync with the datasource (if you could modify the DataKeyArray it would have to propagate to the datasource).
What you need to do is make a deep copy of gv_courses.DataKeys[index].Values and then save that new copy in Session["keys"].
For example:
The keys OrderedDictionary is the same as gv_courses.DataKeys[index].Values but it doesn't have read only. So now you can save it to the session:
And now the code will run since the OrderedDictionary in Session is no longer read only:
我达到了我想要做的,感谢所有回复..
然后
I reach what i want to do ,thanks for all replies ..
Then