Continued from page 1
Category.DataBind();
... other c# code ...
In your .aspx file you will have to change DataSource to your child repeater (SubCategory) and syntax in which you get data.
The code below does so:
... other HTML code ...
<%@ Import Namespace="System.Data" %>
<asp:Repeater Runat="server" ID="Category">
<ItemTemplate>
<a href="category.aspx?category=<%# DataBinder.Eval(Container.DataItem, " rel="nofollow"Category") %>"><%# DataBinder.Eval(Container.DataItem, "Category") %></a><br>
<asp:Repeater Runat="server" ID="SubCategory" DataSource='<%# ((DataRowView)Container.DataItem).Row.GetChildRows("CategoryRelation")%>>
<ItemTemplate>
<a href="category.aspx?category=<%# DataBinder.Eval(Container.DataItem, " rel="nofollow"["SubCategory"]") %>"><%# DataBinder.Eval(Container.DataItem, "["SubCategory"]") %></a><br>
</ItemTemplate>
</asp:Repeater>
</ItemTemplate>
</asp:Repeater>
... other HTML code ...
If you do both changes, code should work fine.
Another interesting thing to note is how you can access a dataitem from parent (from within your child).
For example, we wish link to include Category also, even if it is a SubCategory.
Adjust your .aspx file to following:
... other HTML code ...
<%@ Import Namespace="System.Data" %>
<asp:Repeater Runat="server" ID="Category">
<ItemTemplate>
<a href="category.aspx?category=<%# DataBinder.Eval(Container.DataItem, " rel="nofollow"Category") %>"><%# DataBinder.Eval(Container.DataItem, "Category") %></a><br>
<asp:Repeater Runat="server" ID="SubCategory" DataSource='<%# ((DataRowView)Container.DataItem).Row.GetChildRows("CategoryRelation")%>>
<ItemTemplate>
<a href="category.aspx?category=<%# DataBinder.Eval(Container.DataItem, " rel="nofollow"["SubCategory"]") %>"><%# ((DataRow)Container.DataItem).GetParentRow("CategoryRelation")["Category"] %> - <%# DataBinder.Eval(Container.DataItem, "["SubCategory"]") %></a><br>
</ItemTemplate>
</asp:Repeater>
</ItemTemplate>
</asp:Repeater>
... other HTML code ...
This code is both easy to maintain, and performant.
The same principles count for other items bound to database data, like DataGrid for example.
View our large database of webmaster and programming tutorials and articles.