【发布时间】:2014-03-30 10:27:38
【问题描述】:
我正在尝试使用转发器填充 <li> 项目,并将其绑定到 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>
这里有什么问题?
【问题讨论】:
-
当绑定到
Dictionary<string, string>时,您的 ItemTemplate 对我有效。您确定该错误与 ItemTemplate 中的绑定有关吗?您有 OnItemDataBound 事件处理程序吗?字典的类型是Dictionary<string, string>吗?可以添加错误的 StackTrace 吗? -
Markus 感谢您检查我的代码,您是正确的,错误与 ItemTemplate 无关。我错误地将 DataSource 设置为字典的值而不是字典本身。非常感谢!现在可以了!
-
@user26333600:很高兴听到它有效。我已经添加了答案。
标签: c# asp.net dictionary casting repeater