千家信息网

Repeater如何绑定dictionary数据源

发表于:2025-02-02 作者:千家信息网编辑
千家信息网最后更新 2025年02月02日,这篇文章给大家分享的是有关Repeater如何绑定dictionary数据源的内容。小编觉得挺实用的,因此分享给大家做个参考,一起跟随小编过来看看吧。.aspx页面代码复制代码 代码如下:<%# ((
千家信息网最后更新 2025年02月02日Repeater如何绑定dictionary数据源

这篇文章给大家分享的是有关Repeater如何绑定dictionary数据源的内容。小编觉得挺实用的,因此分享给大家做个参考,一起跟随小编过来看看吧。

.aspx页面代码

复制代码 代码如下:




<%# ((KeyValuePair>)Container.DataItem).Key %>



<%# (Container.DataItem as User).Id %>
<%# (Container.DataItem as User).Name %>







.aspx.cs后置代码

复制代码 代码如下:


public partial class Temp : System.Web.UI.Page
{
Dictionary> dict = new Dictionary>();
protected void Page_Load(object sender, EventArgs e)
{
dict.Add(1, new List
{
new User{Id = 1, Name = "aa"}
,new User{Id = 2, Name = "bb"}
});
dict.Add(2, new List
{
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数据源"这篇文章就分享到这里了,希望以上内容可以对大家有一定的帮助,让大家可以学到更多知识,如果觉得文章不错,可以把它分享出去让更多的人看到吧!

0