Repeater如何绑定dictionary数据源

发布时间:2021-10-14 14:07:41 作者:小新
来源:亿速云 阅读:135

这篇文章给大家分享的是有关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数据源”这篇文章就分享到这里了,希望以上内容可以对大家有一定的帮助,让大家可以学到更多知识,如果觉得文章不错,可以把它分享出去让更多的人看到吧!

推荐阅读:
  1. Python 字典(Dictionary)
  2. jQuery 绑定 ComponentOne Wijmo Combobox 数据源

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

repeater dictionary

上一篇:如何实现罗马数字的转化

下一篇:.net中反射的示例分析

相关阅读

您好,登录后才能下订单哦!

密码登录
登录注册
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》