绑定字典中继器 [英] Bind dictionary to repeater

查看:84
本文介绍了绑定字典中继器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个字典对象<字符串,字符串> ,并想将其绑定到一个中继器。但是,我不知道要放什么东西在 ASPX 标记来实际显示键值对。没有引发的错误,我可以得到它以列表工作。我如何获得一个字典中的中继器显示?

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 aspx markup 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?

推荐答案

这是的IDictionary&LT; TKEY的,TValue&GT; 也是一个的ICollection&LT; KeyValuePair&LT; TKEY的,TValue&GT;方式&gt;

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

You need to bind to something like (untested):

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

请注意,在其中返回项的顺序是不确定的。他们很可能在小字典的插入顺序返回,但不能保证。如果你需要一个保证的顺序, SortedDictionary&LT; TKEY的,TValue&GT; 按排序键。

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.

或者,如果你需要一个不同的排序顺序(如按值),你可以创建一个列表&LT; KeyValuePair&LT;字符串,字符串&GT;&GT; 您的键 - 值对,然后对它进行排序,并绑定到排序列表。

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.


我用这个code在标记显示键和值分别:

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") %>

这篇关于绑定字典中继器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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