强类型数据绑定和泛型?

11

假设我想使用新的ASP.NET 4.5强类型数据绑定将一个通用类型(这里是Dictionary<string, string>)绑定到Repeater上。

那么我需要将KeyValuePair<string, string>作为Repeater的ItemType属性。

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

这里显然存在一个问题:我无法在ItemType文本中使用<>标签!

应该如何解决?是否有可能在新的数据绑定模型中以某种方式使用泛型?


尝试使用<和>进行转义了吗?有出现错误信息吗? - sisve
我还没有尝试在运行页面时是否有效,但是VS将其标记为错误,Intellisense也无法工作。 - magnattic
不行,还是无法运行。错误信息显然是VS无法识别该类型。 - magnattic
我看不到这里声明的数据源,所以我假设你是在代码后台中设置它。你为什么不能在那里也设置类型呢? - Sinaesthetic
1个回答

14
这对我有用:
背后的代码。
   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>

这解决了我长期以来的一个问题,谢谢。 - Dart Feld

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接