绑定到项目模板中的字典
我有一个列表<字典<字符串,字符串>>。在 GridView 的 ItemTemplate 中,我想根据键绑定到字典中的值。
<ItemTemplate>
<tr class="gridRow">
<table width="100%">
<tr><td><%# Eval("Lastname") %>, <%# Eval("Firstname") %> </td></tr>
<tr><td><%# Eval("Address") %></td><td><%# Eval("Zipcode") %></td><td><%# Eval("City") %></td></tr>
</table>
</tr>
</ItemTemplate>
无论如何,有没有办法在不使用后台代码的情况下做到这一点?我只想在字典中添加新键时更改 ItemTemplate。
I have a List< Dictionary< string, string>>. In the ItemTemplate of my GridView I would like to bind to the values in the dictionary based on their keys.
<ItemTemplate>
<tr class="gridRow">
<table width="100%">
<tr><td><%# Eval("Lastname") %>, <%# Eval("Firstname") %> </td></tr>
<tr><td><%# Eval("Address") %></td><td><%# Eval("Zipcode") %></td><td><%# Eval("City") %></td></tr>
</table>
</tr>
</ItemTemplate>
Is there anyway to do this without using code behind? I want to only have to change the ItemTemplate when new keys in the dictionary are added.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我花了一秒钟才明白你在做什么。我想你可以这样做,但是当你需要实际评估被绑定的项目时,有更好的结构可以用来绑定。
不管怎样,你遇到的问题是你有一个键和一个值,你没有“名字”,“姓氏”等。是的,我知道你有包含这些值的字符串,但它们没有被命名为值。字典包含一个键值对。
我的解决方案是将数据放入像数据集这样的构造中。然后就可以根据列名进行绑定了。我想你可以正确地抛出键值对,然后按名称获取一个项目,但我还没有尝试过。我看到 HalfTrackMindMan,但我不确定这是否会以声明方式工作。值得一试。
It took me a second to see what you were doing. I would image you can do this, but there are much better constructs to use to bind when you need to actually evaluate the item being bound.
Regardless, the problem you are having is you have a key and a value, you do not have a "Firstname", "Lastname", etc. Yes, I know you have strings that havve those values, but they are not named the values. A Dictionary contains a key value pair.
My solution would be to put the data into a construct like a DataSet. Then you can bind according to the column name. I imagine you can properly cast out the key value pair and then get an item by name, but I have not tried it. I see HalfTrackMindMan, but I am not sure that will work in a declarative fashion. It is worth a try.