当前位置:Gxlcms > asp.net > Repeater绑定dictionary数据源代码及报错解决

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

时间:2021-07-01 10:21:17 帮助过:24人阅读

.aspx页面代码
代码如下:
  1. <br><asp:Repeater ID="Repeater1" runat="server"> <br><ItemTemplate> <br><%# ((KeyValuePair<int, List<User>>)Container.DataItem).Key %> <br /> <br><asp:Repeater ID="Repeater2" runat="server" DataSource='<%# ((KeyValuePair<int, List<User>>)Container.DataItem).Value %>'> <br><ItemTemplate> <br><%# (Container.DataItem as User).Id %> <br><%# (Container.DataItem as User).Name %> <br></ItemTemplate> <br></asp:Repeater> <br><hr /> <br></ItemTemplate> <br></asp:Repeater> <br> <br><strong>.aspx.cs后置代码</strong> <br><span><u></u></span> 代码如下:<pre class="brush:php;toolbar:false layui-box layui-code-view layui-code-notepad"><ol class="layui-code-ol"><li><br>public partial class Temp : System.Web.UI.Page <br>{ <br>Dictionary<int, List<User>> dict = new Dictionary<int, List<User>>(); <br>protected void Page_Load(object sender, EventArgs e) <br>{ <br>dict.Add(1, new List<User> <br>{ <br>new User{Id = 1, Name = "aa"} <br>,new User{Id = 2, Name = "bb"} <br>}); <br>dict.Add(2, new List<User> <br>{ <br>new User{Id = 3, Name = "cc"} <br>,new User{Id = 4, Name = "dd"} <br>}); <br>Repeater1.DataSource = dict; <br>Repeater1.DataBind(); <br>} <br>} <br>public class User <br>{ <br>public int Id{get;set;} <br>public string Name{get;set;} <br>} <br> <br><strong>如果报以下错误</strong>: <br>repeater 使用的是无效数据源。有效数据源必须实现 IListSource 或 IEnumerable? <br>是因为数据源类型问题,比如 DataTable DataSet Xml Arrry 集合 <br>像 String int 对象 这样的类型是不能直接作为它的数据源的,尤其要注意对象引起的问题</li><li> </li><li> </li></ol></pre>

人气教程排行