nested dictionary to nested repeater asp.net c#
Solution 1
If you do indeed need to use nested repeaters, its possible, but getting it working isn't particularly obvious. The following would work (simplified for brevity):
<asp:Repeater id="level1" OnItemDataBound="level1_ItemDataBound" ...>
<ItemTemplate>
<asp:Repeater id="level2" OnItemDataBound="level2_ItemDataBound" ...>
<ItemTemplate>
<asp:Repeater id="level3" OnItemDataBound="level3_ItemDataBound" ...>
</asp:Repeater>
</ItemTemplate>
</asp:Repeater>
</ItemTemplate>
</asp:Repeater>
Code behind:
protected void Page_Load(object sender, EventArgs e)
{
Dictionary<string, Dictionary<string, List<info>>> branch = ...;
level1.DataSource = branch;
level1.DataBind();
}
protected void level1_ItemDataBound(object sender, RepeaterItemEventArgs e)
{
Dictionary<string, List<info>> data = (Dictionary<string, List<info>>)e.Item.DataItem;
Repeater level2 = e.Item.FindControl("level2") as Repeater;
level2.DataSource = data;
level2.DataBind();
}
protected void level2_ItemDataBound(object sender, RepeaterItemEventArgs e)
{
List<info> data = (List<info>)e.Item.DataItem;
Repeater level3 = e.Item.FindControl("level3") as Repeater;
level3.DataSource = data;
level3.DataBind();
}
protected void level3_ItemDataBound(object sender, RepeaterItemEventArgs e)
{
// Bind properties from info elements in List<info> here
}
Solution 2
There's an nicer way of doing this without any ItemDataBound events. For simplicity let's assume two level of repeaters.
<asp:Repeater id="level1" >
<ItemTemplate>
<asp:Repeater id="level2"
DataSource='<%# ((System.Collections.Generic.KeyValuePair<string,System.Collections.Generic.List<string>>)Container.DataItem).Value %>' >
<ItemTemplate>
<%# Container.DataItem %>
</ItemTemplate>
</asp:Repeater>
</ItemTemplate>
</asp:Repeater>
I usually prefer this way, since for some reason I hate ItemDataBound events :)
Solution 3
asp:TreeView? Not sure how it will handle the nested data set but it's designed to display it.
Admin
Updated on June 22, 2022Comments
-
Admin almost 2 years
I'm making an asp.page that will display hierarchical information about company assets.
To grab the data I used a lambda expression:
FASAssetInfoDataContext fasInfo = new FASAssetInfoDataContext(); var data = from g in fasInfo.Asset_Informations where g.Department.Substring(0, 3) == p select g; Dictionary<string, Dictionary<string, List<info>>> branch = data.GroupBy(e => e.Location) .ToDictionary(g => g.Key, g => g.GroupBy(gl => gl.G_L_Asset_Acct_No) .ToDictionary(gl => gl.Key, gl => gl.Select(s => new info { acqDate = s.Acquisition_Date, asstNo = s.Co_Asset_Number, classInfo = s.Class, description = s.Description, mfgSerialNo = s.Mfg_Serial_No, deprThisRun = s.Depr_This_Run__Int_, AcqValue = s.Acquisition_Value__Int_, currentAccDepr = s.Current_Accum_Depr__Int_, estLife = s.Est_Life__YYMM___Int_, inServiceDate = s.Placed_In_Service_Date__Int_, netBookValue = s.Current_Net_Book_Value__Int_, oldAcqValue = s.Acquisition_Value__Tax_ }).ToList()));
So I now have a nested set of dictionaries with a list of information at the end. My question is how do I best display this information on the page itself? I can get the first level but have been struggling to get the nested repeater to function properly. If there is a better control to use I'm all ears :)
Thanks,
Marco
-
Admin almost 15 yearsThat's not a bad idea. I'll try that out and see if it does what I need it to. Thanks
-
Admin almost 15 yearsThanks! I got the nested repeaters working. Now i just need to format them :)
-
heisenberg almost 14 yearsI wish I could give more than +1, I cannot tell you how much this helped me.
-
Jubal over 13 yearsMuch appreciated HashName! This is exactly what I was looking for.
-
Brad8118 about 13 yearsI"ll throw a +1 on too. Exactly what I'm looking for.
-
Jonathan Williams almost 13 yearsThank you for this code sample - it worked a treat! I also output the Dictionary's key in the outer repeater using
code
<h2><%# ((KeyValuePair<string,List<string>>)Container.DataItem).Key %></h2>code
-
Chris over 11 yearsThis is great. For my needs I just wanted to iterate child objects (each compliance level has a list of compliance tags). It works like this:
((ComplianceLevel)Container.DataItem).ComplianceTagList.OrderBy(ct => ct.Name)