Repeater绑定dictionary数据源代码及报错解决

发布时间:2022-04-16 发布网站:脚本宝典
脚本宝典收集整理的这篇文章主要介绍了Repeater绑定dictionary数据源代码及报错解决脚本宝典觉得挺不错的,现在分享给大家,也给大家做个参考。
.aspx页面代码
复制代码 代码如下:

<asp:RePEater ID="Repeater1" runat="server">
<ITemTemplate>
<%# ((KeyValuePair<int, List<User>>)Container.DataItem).Key %> <br />
<asp:Repeater ID="Repeater2" runat="server" DataSource='<%# ((KeyValuePair<int, List<User>>)Container.DataItem).Value %>'>
<ItemTemplate>
<%# (Container.DataItem as User).Id %>
<%# (Container.DataItem as User).Name %>
</ItemTemplate>
</asp:Repeater>
<hr />
</ItemTemplate>
</asp:Repeater>

.aspx.cs后置代码
复制代码 代码如下:

public partial class Temp : System.Web.UI.Page
{
Dictionary<int, List<User>> dict = new Dictionary<int, List<User>>();
PRotected void Page_Load(object sender, Eventargs e)
{
dict.Add(1, new List<User>
{
new User{Id = 1, Name = "aa"}
,new User{Id = 2, Name = "bb"}
});
dict.Add(2, new List<User>
{
new User{Id = 3, Name = "cc"}
,new User{Id = 4, Name = "dd"}
});
Repeater1.DataSource = dict;
Repeater1.DataBind();
}
}
public class User
{
public int Id{get;set;}
public string Name{get;set;}
}

如果报以下错误:
repeater 使用的是无效数据。有效数据源必须实现 IListSource 或 IEnumerable?
是因为数据源类型问题,比如 DataTable DataSet XMl Arrry 集合
像 String int 对象 这样的类型是不能直接作为它的数据源的,尤其要注意对象引起的问题

脚本宝典总结

以上是脚本宝典为你收集整理的Repeater绑定dictionary数据源代码及报错解决全部内容,希望文章能够帮你解决Repeater绑定dictionary数据源代码及报错解决所遇到的问题。

如果觉得脚本宝典网站内容还不错,欢迎将脚本宝典推荐好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。