c# - 将字典绑定(bind)到转发器 asp.net 时出现错误

标签 c# asp.net dictionary casting repeater

我正在尝试填充 <li>带有转发器的项目,将其绑定(bind)到 Dictionary 对象。我有一本名为 cart 的字典,它具有标准的 Key、Value 属性。

我收到一个运行时错误:

`System.InvalidCastException: Specified cast is not valid.`

我使用了这段代码:

<ItemTemplate>
<li id="<%# ((KeyValuePair<string,string>)Container.DataItem).Key %>_i" >
<%# ((KeyValuePair<string,string>)Container.DataItem).Key %> <%# ((KeyValuePair<string,string>)Container.DataItem).Value %>
</li></ItemTemplate>

这里有什么问题吗?

最佳答案

当绑定(bind)到 Dictionary<string, string> 时,您的 ItemTemplate 对我有用.因此,我建议检查 Repeater 是否真的绑定(bind)到 Dictionary<string, string>。 .

如果不是,异常是有意义的,可以通过修复数据绑定(bind)代码(或更改 ItemTemplate 中使用的类型,使其与绑定(bind)到 Repeater 的数据相匹配)来解决。

关于c# - 将字典绑定(bind)到转发器 asp.net 时出现错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22060951/

相关文章:

c# - 具有弹性池的 Entity Framework 。如何管理我的 SaaS 客户数据库?

ruby - 字典=哈希?

c# - 将表达式<T, bool> 转换为字符串

c# - 在 C# 中访问类文件中的母版页

c# - 在 C# 中将 asn1 转换为 base64

c# - 用户代码未处理 SqlException

arrays - 在 Swift 中将字符串缩减为字典

python - 字典上的列表理解给出空列表

c# GMT 时间等同于 UTC 时间?

c# - 检测字符串是否包含大写字符