OrderedDictionary 是只读的,无法修改

发布于 2024-11-19 06:19:15 字数 545 浏览 5 评论 0原文

问:

当我尝试以下代码时:

 ((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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

等你爱我 2024-11-26 06:19:15

强力解决方案是在保存到会话之前将值复制到新字典:

IOrderedDictionary orderedDictionary = new OrderedDictionary();

foreach (KeyValuePair<TKey, TValue> kvp in gv_courses.DataKeys[index].Values)
{
    orderedDictionary.Add(kvp.key, kvp.value);
}

Session["Keys"] = keys;

但是,我强烈建议找到另一种方法来获取密钥,而不使用 GridView 的 DataKeys 属性。

Brute force solution is to copy values to new dictionary before saving to session:

IOrderedDictionary orderedDictionary = new OrderedDictionary();

foreach (KeyValuePair<TKey, TValue> kvp in gv_courses.DataKeys[index].Values)
{
    orderedDictionary.Add(kvp.key, kvp.value);
}

Session["Keys"] = keys;

However, I strongly recommend to find another way to get your keys without using DataKeys property of your GridView.

天荒地未老 2024-11-26 06:19:15
  • 为什么只是只读?

该代码给出只读错误,因为 Session["Keys"] 中的引用指向只读的 gv_courses.DataKeys[index].Values 。整个 DataKeyArray Datakeys 以及每个 DataKey 元素的 Values 属性都是只读的。从我在互联网上读到的内容来看,这是一个 .net 设计选择,它可能是只读的,因此它与数据源保持同步(如果您可以修改 DataKeyArray,它必须传播到数据源)。

  • 如何在会话中设置我的密钥,解决我的问题?

您需要做的是制作 gv_courses.DataKeys[index].Values 的深层副本,然后将该新副本保存在 Session["keys"] 中。
例如:

IOrderedDictionary keys = gv_courses.DataKeys[index].Values;
var keysTemp = new OrderedDictionary();
foreach (DictionaryEntry de in keys)
{
    keysTemp.Add(de.Key, de.Value);
}

键 OrderedDictionary 与 gv_courses.DataKeys[index].Values 相同,但它不是只读的。现在您可以将其保存到会话中:

Session["KeysCourse"] = keysTemp; //keysTemp is equal to keys but is editable

现在代码将运行,因为会话中的 OrderedDictionary 不再是只读的:

((IOrderedDictionary)Session["KeysCourse"])[2] = objToUpdate.Note_title;
((IOrderedDictionary)Session["KeysCourse"])[3] = objToUpdate.Iscourse;
  • Why just read only?

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).

  • How to set my key in the session,fixing my problem?

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:

IOrderedDictionary keys = gv_courses.DataKeys[index].Values;
var keysTemp = new OrderedDictionary();
foreach (DictionaryEntry de in keys)
{
    keysTemp.Add(de.Key, de.Value);
}

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:

Session["KeysCourse"] = keysTemp; //keysTemp is equal to keys but is editable

And now the code will run since the OrderedDictionary in Session is no longer read only:

((IOrderedDictionary)Session["KeysCourse"])[2] = objToUpdate.Note_title;
((IOrderedDictionary)Session["KeysCourse"])[3] = objToUpdate.Iscourse;
追我者格杀勿论 2024-11-26 06:19:15

我达到了我想要做的,感谢所有回复..

                IOrderedDictionary keys = gv_courses.DataKeys[index].Values;
                string[] keysTemp = new string[4];
                keysTemp[0] = keys[0].ToString();
                keysTemp[1] = keys[1].ToString();
                keysTemp[2] = keys[2].ToString();
                keysTemp[3] = keys[3].ToString();
                Session["KeysCourse"] = keysTemp;

然后

 ((string[])Session["KeysCourse"])[2] = objToUpdate.Note_title;
 ((string[])Session["KeysCourse"])[3] = objToUpdate.Iscourse.ToString();

I reach what i want to do ,thanks for all replies ..

                IOrderedDictionary keys = gv_courses.DataKeys[index].Values;
                string[] keysTemp = new string[4];
                keysTemp[0] = keys[0].ToString();
                keysTemp[1] = keys[1].ToString();
                keysTemp[2] = keys[2].ToString();
                keysTemp[3] = keys[3].ToString();
                Session["KeysCourse"] = keysTemp;

Then

 ((string[])Session["KeysCourse"])[2] = objToUpdate.Note_title;
 ((string[])Session["KeysCourse"])[3] = objToUpdate.Iscourse.ToString();
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文