强类型数据绑定和仿制药? [英] Strongly typed data binding and generics?
问题描述
假设我要绑定一个泛型类型(这里:词典<字符串,字符串>
)使用新的ASP.NET 4.5直放站强类型数据绑定<。 / P>
然后,我将不得不放下 KeyValuePair&LT;字符串,字符串&GT;
作为直放站的ItemType属性
&LT; ASP:直放站ID =rpCategories=服务器的ItemType =System.Collections.Generic.KeyValuePair&LT;字符串,字符串&GT;&GT;
有一个明显的问题就在这里:我不能使用&LT;
或&GT;
内的ItemType的文字!
如何将一去吗?是使用泛型可能以某种方式与新的数据绑定模式?
这对我的作品:
$ C $后面
ç 保护无效的Page_Load(对象发件人,EventArgs的)
{
rpCategories.DataSource =新字典&LT;字符串,字符串&GT;()
{
{1,项目},{2,项目},{3,项目},
};
rpCategories.DataBind();
}
标记
&LT; ASP:直放站ID =rpCategories=服务器的ItemType =System.Collections.Generic.KeyValuePair`2 [System.String,System.String]&GT ;
&LT;的ItemTemplate&GT;
&LT; ASP:标签ID =标签1=服务器文本='&LT;%#Item.Key%&GT;'&GT;&LT; / ASP:标签&gt;
&LT; / ItemTemplate中&GT;
&LT; / ASP:直放站&GT;
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?
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>
这篇关于强类型数据绑定和仿制药?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!