Repeater绑定dictionary数据源代码及报错解决
来源:
中文源码网 浏览:273 次 日期:2024-05-11 23:02:23
【下载文档: Repeater绑定dictionary数据源代码及报错解决.txt 】
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 对象 这样的类型是不能直接作为它的数据源的,尤其要注意对象引起的问题