问题 强类型数据绑定和泛型?


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

然后我不得不放下 KeyValuePair<string, string> 作为Repeater的ItemType属性。

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

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

怎么会这样呢?使用新的数据绑定模型是否可能以某种方式使用泛型?


2113
2017-09-03 15:40


起源

试图用&lt;逃避它们和&gt;?任何错误消息? - sisve
我没有尝试过,如果在运行页面时它会起作用,但VS将其标记为错误,而Intellisense也不起作用。 - magnattic
不,也不跑。错误消息显然是VS无法识别类型。 - magnattic
我没有看到这里声明的数据源,所以我假设你在后面的代码中设置它。你也不能在那里设置类型吗? - Sinaesthetic
我试过了,没用。 - magnattic


答案:


这对我有用:

代码背后

   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>

12
2017-09-03 16:53



谢谢,这有效! - magnattic
这解决了我长期以来遇到的问题,谢谢。 - Dart Feld