Strongly typed data binding and generics?

*爱你&永不变心* 提交于 2019-12-09 05:37:56

问题


Suppose I want to bind a generic type (here: Dictionary<string, string>) to a Repeater using the new ASP.NET 4.5 strongly typed data binding.

Then I would have to put down KeyValuePair<string, string> as the ItemType Property of the Repeater.

<asp:Repeater id="rpCategories" runat="server" ItemType="System.Collections.Generic.KeyValuePair<string, string>">

There is an obvious problem here: I can not use < or > within the ItemType text!

How would one go about this? Is the use of generics possible somehow with the new data binding model?


回答1:


This works for me:

Code behind

   protected void Page_Load(object sender, EventArgs e)
        {
           rpCategories.DataSource = new Dictionary<string, string>()
            {
                {"1", "item"},{"2", "item"},{"3", "item"},
            };
        rpCategories.DataBind();
        }

Markup

<asp:Repeater ID="rpCategories" runat="server" ItemType="System.Collections.Generic.KeyValuePair`2[System.String,System.String]">
        <ItemTemplate>
            <asp:Label ID="Label1" runat="server" Text='<%# Item.Key %>'></asp:Label>
        </ItemTemplate>
    </asp:Repeater>


来源:https://stackoverflow.com/questions/12250777/strongly-typed-data-binding-and-generics

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!