Nested dictionary for asp.net c # nested relay

I create an asp.page where hierarchical information about company assets will be displayed.

To capture the data, I used the 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 now I have a nested set of dictionaries with a list of information at the end. My question is: what is the best way to display this information on the page itself? I can get the first level, but I'm struggling to set up the enclosed repeater correctly. If there is a better control to use, I'm all ears :)

Thanks,

Marco

+7
dictionary nested repeater
source share
3 answers

If you really need to use nested repeaters, its possible, but getting it working is not particularly obvious. The following will 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 } 
+15
source share

There will be a nicer way to do this without any ItemDataBound events. For simplicity, we allow two levels 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 method, because for some reason I hate ItemDataBound events :)

+9
source share

asp: TreeView? Not sure how it will handle a set of nested data, but it is designed to display it.

+1
source share

All Articles