E
Eirik Eldorsen
Im trying to make a nested repeater with 3 levels. I've successfully created
a nested repeater with 2 levels, but when adding the 3rd level I get an
InvalidCastException. What am I doing wrong?
Here is my code:
<asp:Repeater id="level1Repeater" runat="server">
<itemtemplate>
<a href="#"><%# DataBinder.Eval(Container.DataItem, "Title") %></a>
<br>
<asp:repeater id="level2Repeater" runat="server" datasource='<%#
((DataRowView)Container.DataItem).Row.GetChildRows("L1L2") %>' >
<itemtemplate>
<a href="#"><%# DataBinder.Eval(Container.DataItem,
"[\"Title\"]")%></a>
<br>
<asp:repeater id="level3Repeater" runat="server"
datasource='<%# ((DataRowView)Container.DataItem).Row.GetChildRows("L2L3")
%>' >
<itemtemplate>
<a href="#"><%#
DataBinder.Eval(Container.DataItem, "[\"Title\"]")%><br></a>
</itemtemplate>
</asp:Repeater>
</itemtemplate>
</asp:Repeater>
</itemtemplate>
</asp:Repeater>
Code Behind:
private void BindWebsites()
{
level1Repeater.DataSource = Websites.GetAll();
Page.DataBind();
}
public static DataTable GetAll()
{
.......
ds.Relations.Add("L1L2", ds.Tables["WebsitesL1"].Columns["ID"],
ds.Tables["WebsitesL2"].Columns["ParentID"]);
ds.Relations.Add("L2L3", ds.Tables["WebsitesL2"].Columns["ID"],
ds.Tables["WebsitesL3"].Columns["ParentID"]);
return ds.Tables["WebsitesL1"];
}
a nested repeater with 2 levels, but when adding the 3rd level I get an
InvalidCastException. What am I doing wrong?
Here is my code:
<asp:Repeater id="level1Repeater" runat="server">
<itemtemplate>
<a href="#"><%# DataBinder.Eval(Container.DataItem, "Title") %></a>
<br>
<asp:repeater id="level2Repeater" runat="server" datasource='<%#
((DataRowView)Container.DataItem).Row.GetChildRows("L1L2") %>' >
<itemtemplate>
<a href="#"><%# DataBinder.Eval(Container.DataItem,
"[\"Title\"]")%></a>
<br>
<asp:repeater id="level3Repeater" runat="server"
datasource='<%# ((DataRowView)Container.DataItem).Row.GetChildRows("L2L3")
%>' >
<itemtemplate>
<a href="#"><%#
DataBinder.Eval(Container.DataItem, "[\"Title\"]")%><br></a>
</itemtemplate>
</asp:Repeater>
</itemtemplate>
</asp:Repeater>
</itemtemplate>
</asp:Repeater>
Code Behind:
private void BindWebsites()
{
level1Repeater.DataSource = Websites.GetAll();
Page.DataBind();
}
public static DataTable GetAll()
{
.......
ds.Relations.Add("L1L2", ds.Tables["WebsitesL1"].Columns["ID"],
ds.Tables["WebsitesL2"].Columns["ParentID"]);
ds.Relations.Add("L2L3", ds.Tables["WebsitesL2"].Columns["ID"],
ds.Tables["WebsitesL3"].Columns["ParentID"]);
return ds.Tables["WebsitesL1"];
}