2012-09-03 121 views
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的文字中使用<>

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

+0

试图与<和>逃跑呢?任何错误消息? – sisve

+0

我还没有尝试过,如果这可以在运行页面时起作用,但VS将其标记为错误,并且智能感知也不起作用。 – magnattic

+0

不,也不运行。错误信息显然是VS无法识别的类型。 – magnattic

回答

12

这个工作对我来说:

代码背后

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

谢谢,这工作! – magnattic

+0

这解决了我很长一段时间的问题,谢谢。 –