SoFunction
Updated on 2025-03-09

Repeater binding dictionary data source code and error-reporting solution

.aspx page code
Copy the codeThe code is as follows:

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

.Post-code
Copy the codeThe code is as follows:

public partial class Temp :
{
Dictionary<int, List<User>> dict = new Dictionary<int, List<User>>();
protected void Page_Load(object sender, EventArgs e)
{
(1, new List<User>
{
new User{Id = 1, Name = "aa"}
,new User{Id = 2, Name = "bb"}
});
(2, new List<User>
{
new User{Id = 3, Name = "cc"}
,new User{Id = 4, Name = "dd"}
});
= dict;
();
}
}
public class User
{
public int Id{get;set;}
public string Name{get;set;}
}

If the following error is reported:
The repeater uses an invalid data source. A valid data source must implement IListSource or IEnumerable?
This is because of data source type problems, such as DataTable DataSet Xml Arrry collection
Types like String int objects cannot be directly used as their data source, especially pay attention to the problems caused by objects.