C# 将字典绑定到中继器

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2274875/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-07 00:57:45  来源:igfitidea点击:

Bind dictionary to repeater

c#asp.netdata-bindingdictionaryrepeater

提问by XSL

I have a dictionary object <string, string>and would like to bind it to a repeater. However, I'm not sure what to put in the aspxmarkup to actually display the key-value pair. There are no errors thrown and I can get it to work with a List. How do I get a dictionary to display in a repeater?

我有一个字典对象<string, string>,想将它绑定到中继器。但是,我不确定在aspx标记中放什么来实际显示键值对。没有抛出任何错误,我可以让它与List. 如何让字典显示在中继器中?

采纳答案by Joe

An IDictionary<TKey,TValue>is also an ICollection<KeyValuePair<TKey, TValue>>.

AnIDictionary<TKey,TValue>也是ICollection<KeyValuePair<TKey, TValue>>.

You need to bind to something like (untested):

您需要绑定到类似(未经测试)的东西:

((KeyValuePair<string,string>)Container.DataItem).Key
((KeyValuePair<string,string>)Container.DataItem).Value

Note that the order in which the items are returned is undefined. They may well be returned in the insertion order for small dictionaries, but this is not guaranteed. If you need a guaranteed order, SortedDictionary<TKey, TValue>sorts by key.

请注意,项目返回的顺序是未定义的。对于小型字典,它们很可能会按插入顺序返回,但这并不能保证。如果您需要有保证的订单,请SortedDictionary<TKey, TValue>按键排序。

Or if you need a different sort order (e.g. by value), you could create a List<KeyValuePair<string,string>>of your key-value pairs, then sort it, and bind to the sorted list.

或者,如果您需要不同的排序顺序(例如按值),您可以创建一个List<KeyValuePair<string,string>>键值对,然后对其进行排序,并绑定到排序列表。

Answer: I used this code in the markup to display the key and value individually:

:我在标记中使用此代码分别显示键和值:

<%# DataBinder.Eval((System.Collections.Generic.KeyValuePair<string, string>)Container.DataItem,"Key") %>
<%# DataBinder.Eval((System.Collections.Generic.KeyValuePair<string, string>)Container.DataItem,"Value") %>

回答by Jason Berkan

Bind to the values collection of the dictionary.

绑定到字典的值集合。

myRepeater.DataSource = myDictionary.Values
myRepeater.DataBind()

回答by Arun Krish

<%# Eval("key")%>worked for me.

<%# Eval("key")%>为我工作。

回答by JMD

Write a property in your code-behind of the type of an entry in your bound dictionary. So say, for example, I am binding a Dictionary<Person, int>to my Repeater. I would write (in C#) a property like this in my code-behind:

在绑定字典中的条目类型的代码隐藏中编写一个属性。所以说,例如,我将 a 绑定Dictionary<Person, int>到我的中继器。我会在我的代码隐藏中(在 C# 中)写一个这样的属性:

protected KeyValuePair<Person, int> Item
{
    get { return (KeyValuePair<Person, int>)this.GetDataItem(); }
}

Then, in my view I can use code segments like this:

然后,在我看来,我可以使用这样的代码段:

<span><%# this.Item.Key.FirstName %></span>
<span><%# this.Item.Key.LastName %></span>
<span><%# this.Item.Value %></span>

This makes for much cleaner markup. And while I would prefer less generic names for the values being referenced, I do know that Item.Keyis a Personand Item.Valueis an intand they are strongly-typed as such.

这使得标记更清晰。虽然我更喜欢被引用的值的通用名称,但我知道它Item.Key是 aPersonItem.Valueis anint并且它们是强类型的。

You can (read: should), of course, rename Itemto something more symbolic of an entry in your dictionary. That alone will help reduce any ambiguity in the naming in my example usage.

当然,您可以(阅读:should)重命名Item为更具象征意义的字典条目。仅此一项就有助于减少我的示例用法中命名中的任何歧义。

There is certainly nothing to prevent you from defining an additional property, say like this:

当然没有什么可以阻止你定义一个额外的属性,像这样说:

protected Person CurrentPerson
{
    get { return ((KeyValuePair<Person, int>)this.GetDataItem()).Key; }
}

And using it in your markup thusly:

并因此在您的标记中使用它:

<span><%# this.CurrentPerson.FirstName %></span>

...none of which prevents you from also accessing the corresponding dictionary entry's .Value.

...这些都不会阻止您访问相应字典条目的.Value.