Try to search your question here, if you can't find : Ask Any Question Now ?

How to get value of parent repeater?

HomeCategory: stackoverflowHow to get value of parent repeater?
Avatarsourav asked 4 months ago

I have 3 nested repeaters:

<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>

I’m trying to bind json to these repeaters. The json object looks like the following:

{
  "categories": [
    "Category1":  {
      "categories": [
        "Subcategory 1",
        "Subcategory 2": {
          "categories": [
            "Subsubcategory 1”
            "Subsubcategory 2"
          ]
        }
      ]
    }
  ]
}

For the top category, I bind like so:

Dim categories = loader.LoadFromFile(filePath)
lev_1_nav.DataSource = categories
lev_1_nav.DataBind()

And this works fine, but for binding subcategories I want to do something like this:

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

For the line Dim name As String = (e.Item.Parent.Parent As RepeaterItem).DataItem("Name") it’s saying Value of type 'Control' cannot be converted to 'String'. Although even if that were correct, I’m not sure if I’m even on the right track of binding

1 Answers
Best Answer
AvatarAmit answered 4 months ago
Your Answer

2 + 19 =

Popular Tags

WP Facebook Auto Publish Powered By : XYZScripts.com