Answered by:
Help!: ASP.net Repeater nested within another repeater not displaying the correct category's data

Question
-
User277576413 posted
ASP.net Repeater nested within another repeater not displaying the correct category's data.
just seems to populate the last parent category's subcategory data in all the <li><li> elements:
my html:
<asp:Repeater ID="Repeater1" runat="server" DataSourceID="SqlDataSource2">
<itemtemplate>
<div class="panel panel-default">
<div class="panel-heading">
<h4 class="panel-title">
<a data-toggle="collapse" data-parent="#accordian" href="#sportswear">
<span class="badge pull-right"><i class="fa fa-plus"></i></span>
<%# DataBinder.Eval(Container.DataItem, "PRODUCT_TYPE") %>
<asp:HiddenField ID="HiddenField1" value='<%# DataBinder.Eval(Container.DataItem, "PRODUCT_TYPE") %>' runat="server" />
</a>
</h4>
</div>
<div id="<%# DataBinder.Eval(Container.DataItem, "PRODUCT_TYPE") %>" class="panel-collapse collapse">
<div class="panel-body">
<ul>
<asp:Repeater ID="Repeater2" runat="server">
<itemtemplate>
<li><a><%# Eval("PRODUCT_SUBTYPE") %></a></li>
</itemtemplate>
</asp:Repeater>
</ul>
</div>
</div>
</div>
</itemtemplate>
</asp:Repeater>My function to Bind the nested repeaters data:
Protected Sub Repeater1_ItemDataBound(sender As Object, e As RepeaterItemEventArgs) Handles Repeater1.ItemDataBound
Dim Sqlstr As String
Dim _repeater_counter As Long
If e.Item.ItemType = ListItemType.Item OrElse e.Item.ItemType = ListItemType.AlternatingItem Then
Dim Prod As String = TryCast(e.Item.FindControl("HiddenField1"), HiddenField).Value
Sqlstr = "SELECT DISTINCT [PRODUCT_TYPE], [PRODUCT_SUBTYPE] FROM [SKU_WEB] WHERE ([PRODUCT_TYPE] = '" & Prod & "') ORDER BY [PRODUCT_SUBTYPE]"
SqlDataSource3.SelectCommand = Sqlstr
Dim Rep2 As Repeater = TryCast(e.Item.FindControl("Repeater2"), Repeater)
Rep2.DataSourceID = "SqlDataSource3"
Rep2.DataBind()
End If
End SubWednesday, June 1, 2016 10:40 PM
Answers
-
User277576413 posted
Hi Yohann,<br>
<br>
The nested repeater does databind and display data, however it displays the last parent category's data for All the nested categories.<br>
<br>
I think that the itembound event of the parent repeater fires for each parent category and then renders on when it has finished so that it displays databind on sqldatasource3 right at the end rather than after it has databound on each item.<br>
<br>
After almost head butting the wall after afew hours it appears that this method of using sqldatasource and hidden field to pass parameters is not the way to solve this.<br>
<br>
Data must be pulled, relationship created in code and then binding(s) executed in code, the example in mikesdotnetting.com article ended up helping me solve this.<br>
<br>
http://www.mikesdotnetting.com/article/57/displaying-one-to-many-relationships-with-nested-repeaters<br>
<br>
I hope it helps any others who are struggling with the same problem.<br>
<br>
Thanks for the links Yohann, I did come across the same articles before I posted the thread.- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Friday, June 3, 2016 2:14 AM
All replies
-
User36583972 posted
Hi TheQman,
Rep2.DataSourceID = "SqlDataSource3"May be it is not correct, you should change it as Rep2.DataSourceID = SqlDataSource3. Then the following tutorials introduce how to implement Nested Repeater, you can refer them and test on your side.
1: Implement Nested Repeater (Repeater inside Repeater) with example in ASP.Net using C# and VB.Net:
2: A quick guide to using nested repeaters in ASP.NET:
http://www.codeproject.com/Articles/6140/A-quick-guide-to-using-nested-repeaters-in-ASP-NET
Best Regards,
Yohann Lu
Thursday, June 2, 2016 7:52 AM -
User277576413 posted
Hi Yohann,<br>
<br>
The nested repeater does databind and display data, however it displays the last parent category's data for All the nested categories.<br>
<br>
I think that the itembound event of the parent repeater fires for each parent category and then renders on when it has finished so that it displays databind on sqldatasource3 right at the end rather than after it has databound on each item.<br>
<br>
After almost head butting the wall after afew hours it appears that this method of using sqldatasource and hidden field to pass parameters is not the way to solve this.<br>
<br>
Data must be pulled, relationship created in code and then binding(s) executed in code, the example in mikesdotnetting.com article ended up helping me solve this.<br>
<br>
http://www.mikesdotnetting.com/article/57/displaying-one-to-many-relationships-with-nested-repeaters<br>
<br>
I hope it helps any others who are struggling with the same problem.<br>
<br>
Thanks for the links Yohann, I did come across the same articles before I posted the thread.- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Friday, June 3, 2016 2:14 AM