假设我想使用新的 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/