我有3个嵌套的中继器:
<ul class="sub-menu">
<asp:Repeater id="lev_1_nav" onitemdatabound="lev_1_nav_prod_dataBind" runat="server" >
<ItemTemplate>
<li class="nav-item"><span class="title"><%# Eval("Name") %></span>
<ul class="sub-menu" id="sub_level_2" runat="server">
<asp:Repeater id="level_2_nav_prod" runat="server">
<ItemTemplate>
<li id="li" runat="server">
<span class="title"><%# Eval("Name") %</span>
<ul class="sub-menu" id="sub_level_3" runat="server" visible="False">
<asp:Repeater id="level_3_nav_prod" runat="server">
<ItemTemplate>
<li class="nav-item">
<span class="title"><%# Eval("Name") %></span></a>
</li>
</ItemTemplate>
</asp:Repeater>
</ul>
</li>
</ItemTemplate>
</asp:Repeater>
</ul>
</li>
</ItemTemplate>
</asp:Repeater>
</ul>
我正在尝试将json绑定到这些转发器。 json对象如下所示:
{
"categories": [
"Category1": {
"categories": [
"Subcategory 1",
"Subcategory 2": {
"categories": [
"Subsubcategory 1”
"Subsubcategory 2"
]
}
]
}
]
}
对于顶级类别,我这样绑定:
Dim categories = loader.LoadFromFile(filePath)
lev_1_nav.DataSource = categories
lev_1_nav.DataBind()
这很好用,但是对于绑定子类别,我想做这样的事情:
Sub lev_1_nav_prod_dataBind(ByVal Sender As Object, ByVal e As RepeaterItemEventArgs)
If e.Item.ItemType = ListItemType.Item Or e.Item.ItemType = ListItemType.AlternatingItem Then
Dim rep As Repeater = e.Item.FindControl("lev_2_nav_prod")
Dim name As String = (e.Item.Parent.Parent As RepeaterItem).DataItem("Name")
For Each cat In categories
If e.Item.DataItem("Name") = cat.Name Then
rep.DataSource = cat.categories
rep.DataBind()
End if
Next
End If
End Sub
对于行Dim name As String = (e.Item.Parent.Parent As RepeaterItem).DataItem("Name")
,它的意思是Value of type 'Control' cannot be converted to 'String'
。尽管那是正确的,但我不确定我是否在正确的装订轨道上