c# - 将字典绑定(bind)到中继器

标签 c# asp.net data-binding dictionary repeater

我有一个字典对象 <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/

相关文章:

Android 数据绑定(bind)库不适用于最新的 Gradle 版本

c# - MVC 6 中的 CorrelationID 在哪里

c# - ServiceStack.Text 可以将 JSON 反序列化为自定义泛型类型吗?

c# - 在 fiddler core 的新版本 (4.6.3.50306) 中缺少一些首选项

c# - 为什么我的数字从小数转换为 float 时会发生变化?

c# - SQL 和 C# 从数据库检索单个记录

javascript - HTML 中的 Vue JS 无法识别添加的对象和属性

C#/WinRT : How to save image to file

c# - 在服务器端代码 C# 中获取本地日期时间而不是服务器日期时间

java - 使用 Comboviewer 进行 JFace 数据绑定(bind)