Bind dictionary to repeater

前端 未结 4 762
小蘑菇
小蘑菇 2020-12-29 01:53

I have a dictionary object and would like to bind it to a repeater. However, I\'m not sure what to put in the aspx markup to

4条回答
  •  悲哀的现实
    2020-12-29 02:20

    An IDictionary is also an ICollection>.

    You need to bind to something like (untested):

    ((KeyValuePair)Container.DataItem).Key
    ((KeyValuePair)Container.DataItem).Value
    

    Note that the order in which the items are returned is undefined. They may well be returned in the insertion order for small dictionaries, but this is not guaranteed. If you need a guaranteed order, SortedDictionary sorts by key.

    Or if you need a different sort order (e.g. by value), you could create a List> of your key-value pairs, then sort it, and bind to the sorted list.

    Answer: I used this code in the markup to display the key and value individually:

    <%# DataBinder.Eval((System.Collections.Generic.KeyValuePair)Container.DataItem,"Key") %>
    <%# DataBinder.Eval((System.Collections.Generic.KeyValuePair)Container.DataItem,"Value") %>
    

提交回复
热议问题