我有一个字典对象 <string, string>
并想将其绑定(bind)到中继器。但是,我不确定要在 aspx
中放什么实际显示键值对的标记。没有抛出任何错误,我可以让它与 List
一起工作.如何让字典显示在复读机中?
最佳答案
安IDictionary<TKey,TValue>
也是一个 ICollection<KeyValuePair<TKey, TValue>>
.
你需要绑定(bind)到类似(未测试)的东西:
((KeyValuePair<string,string>)Container.DataItem).Key
((KeyValuePair<string,string>)Container.DataItem).Value
请注意,返回项目的顺序是未定义的。它们很可能在小型词典的插入顺序中返回,但这并不能保证。如果您需要保证订单,SortedDictionary<TKey, TValue>
按键排序。
或者如果您需要不同的排序顺序(例如按值),您可以创建 List<KeyValuePair<string,string>>
键值对,然后对其进行排序,并绑定(bind)到排序后的列表。
回答: 我在标记中使用了这段代码来分别显示键和值:
<%# DataBinder.Eval((System.Collections.Generic.KeyValuePair<string, string>)Container.DataItem,"Key") %>
<%# DataBinder.Eval((System.Collections.Generic.KeyValuePair<string, string>)Container.DataItem,"Value") %>
关于c# - 将字典绑定(bind)到中继器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2274875/