[英]How do I use Eval() to reference values in a SortedDictionary in an asp Repeater?
我認為我很聰明地從內存密集型DataView切換到SortedDictionary,以實現內存高效的可排序數據結構。 現在,我不知道如何在<%#或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>
有什么建議么?
使用以下兩個選項之一:
<%# Eval("Key") %>
<!-- or -->
<%# Eval("Value") %>
取決於您需要鍵還是字典中的值。
如果您考慮數據綁定是如何工作的,這是有道理的。 數據綁定是一個過程,控件通過該過程獲取一個可枚舉的序列,對該序列進行迭代,並使用反射根據項目所公開的屬性從序列中的每個項目中提取值。
由於SortedDictionary<TKey,TValue>
實現IEnumerable<KeyValuePair<TKey, TValue>>
接口,您知道轉發器將枚舉KeyValuePair<TKey, TValue>
對象的序列,依次綁定到每個對象。
如果查看由KeyValuePair<TKey,TValue>
類公開的屬性,則只會看到兩個: Key
和Value
。 由於它們是公共屬性,因此您可以Eval
它們,並且控件將能夠提取它們封裝的值。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.