c# - 强类型数据绑定(bind)和泛型?

标签 c# asp.net .net data-binding asp.net-4.5

假设我想使用新的 ASP.NET 4.5 强类型数据绑定(bind)将泛型类型(此处:Dictionary<string, string>)绑定(bind)到 Repeater。

那我就得放下KeyValuePair<string, string>作为 Repeater 的 ItemType 属性。

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

这里有一个明显的问题:我不能使用<>在 ItemType 文本中!

人们会怎么做呢?是否可以通过新的数据绑定(bind)模型以某种方式使用泛型?

最佳答案

这对我有用:

代码隐藏

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

标记

<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>

关于c# - 强类型数据绑定(bind)和泛型?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12250777/

相关文章:

c# - MVC Entity Framework 中的种子方法

c# - 提醒用户注意新记录

c# - .NET 4.0 并发收集性能

c# - .Net Core HttpClient 摘要身份验证

c# - 在没有应用程序的情况下使用 C# 从 facebook 获取我自己的数据

c# - 使用数据列表从 asp.net 中的文件夹绑定(bind)视频

c# - 找不到 Entity Framework 4 注释

.net - 在 .NET 中序列化数据传输对象

c# - 任务取消最佳实践

C# ConvertTimeFromUtc 时髦