gpt4 book ai didi

c# - 如何使用 Eval() 来引用 asp Repeater 中 SortedDictionary 中的值?

转载 作者:行者123 更新时间:2023-11-30 20:13:59 25 4
gpt4 key购买 nike

我认为我很聪明,从内存密集型 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") %>

取决于您是需要字典中的键还是值。

如果您考虑数据绑定(bind)的工作原理,这是有道理的。数据绑定(bind)是一个过程,通过该过程,控件采用可枚举序列、迭代该序列,并使用反射根据项目公开的属性从序列中的每个项目中提取值。

SortedDictionary<TKey,TValue>实现 IEnumerable<KeyValuePair<TKey, TValue>>接口(interface)你知道中继器将枚举一个序列 KeyValuePair<TKey, TValue>对象,依次绑定(bind)到每个对象。

如果您查看 KeyValuePair<TKey,TValue> 公开的属性你只会看到两个类:KeyValue .由于它们是公共(public)属性(property),您可以 Eval它们和控件将能够提取它们封装的值。

关于c# - 如何使用 Eval() 来引用 asp Repeater 中 SortedDictionary 中的值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1104085/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com