开发者

Binding to a dictionary in an 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 anyw开发者_Go百科ay to do this without using code behind? I want to only have to change the ItemTemplate when new keys in the dictionary are added.


<%# ((Dictionary<string, string>)(Container.DataItem))["FirstName"] %>


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜