简体   繁体   English

如何使用Eval()引用ASP中继器中SortedDictionary中的值?

[英]How do I use Eval() to reference values in a SortedDictionary in an asp Repeater?

I thought I was clever to switch from the memory intensive DataView to SortedDictionary as a memory efficient sortable data structure. 我认为我很聪明地从内存密集型DataView切换到SortedDictionary,以实现内存高效的可排序数据结构。 Now I have no idea how get the key and value out of the datasource in the <%# or Eval() expressions. 现在,我不知道如何在<%#或Eval()表达式中从数据源中获取键和值。

SortedDictionary<int, string> data = RetrieveNames();
rCurrentTeam.DataSource = data;
rCurrentTeam.DataBind();

<asp:Repeater ID="rNames" runat="server">
 <ItemTemplate>
  <asp:Label ID="lblName" runat="server" Text='<%# Eval("what?") %>' />
 </ItemTemplate>
</asp:Repeater>

Any suggestions? 有什么建议么?

Use either of these two options: 使用以下两个选项之一:

<%# Eval("Key") %>
<!-- or -->
<%# Eval("Value") %>

depending on whether you need the key or the value from the dictionary. 取决于您需要键还是字典中的值。

This makes sense if you think about how data binding works. 如果您考虑数据绑定是如何工作的,这是有道理的。 Data binding is a process by which a control takes an enumerable sequence, iterates that sequence, and uses reflection to extract values from each item in the sequence based on properties that the item exposes. 数据绑定是一个过程,控件通过该过程获取一个可枚举的序列,对该序列进行迭代,并使用反射根据项目所公开的属性从序列中的每个项目中提取值。

Since SortedDictionary<TKey,TValue> implements the IEnumerable<KeyValuePair<TKey, TValue>> interface you know that the repeater will enumerate a sequence of KeyValuePair<TKey, TValue> objects, binding to each one in turn. 由于SortedDictionary<TKey,TValue>实现IEnumerable<KeyValuePair<TKey, TValue>>接口,您知道转发器将枚举KeyValuePair<TKey, TValue>对象的序列,依次绑定到每个对象。

If you look at the properties that are exposed by the KeyValuePair<TKey,TValue> class you will see just two: Key and Value . 如果查看由KeyValuePair<TKey,TValue>类公开的属性,则只会看到两个: KeyValue Since they are public properties you can Eval them and the control will be able to extract the values that they encapsulate. 由于它们是公共属性,因此您可以Eval它们,并且控件将能够提取它们封装的值。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM