SUN SUN - 4 months ago 53
Vb.net Question

Bind Listview Inside Listview for sub menus(child menus)

I am little confused for creating website menu & sub menus which is completely dynamic & made from database. For Menus I used Listview Control which is fine now all menus have submenus(child menu) as well which is based on menus. Now I don't understand how to do it. I guess using nested ListView would be good for this but I need an hint how to bind nested listview?

This is my main Listview

<asp:ListView ID="mainMenu" runat="server">
<ItemTemplate>
<li><asp:HyperLink ID="mainLinks" runat="server" NavigateUrl='<%# Eval("name", "~/{0}") %>' Text='<%# Eval("name") %>'></asp:HyperLink>
<ul class="super-child">
<asp:ListView ID="childMenu" runat="server">
<ItemTemplate>
<li><asp:HyperLink ID="cat3" runat="server" NavigateUrl='<%# Eval("category") & Eval("name", "/{0}") %>' Text='<%# Eval("name") %>'></asp:HyperLink></li>
</ItemTemplate>
</asp:ListView>
</ul>
</li>
</ItemTemplate>
</asp:ListView>


Private Sub bigMenu()
Dim constr As String = ConfigurationManager.ConnectionStrings("conio").ConnectionString
Using con As New MySqlConnection(constr)
Using cmd As New MySqlCommand()
cmd.CommandText = "SELECT * FROM mainMenu WHERE status = 'active' order by CAST(position as SIGNED INTEGER) asc"
cmd.Connection = con
Using sda As New MySqlDataAdapter(cmd)
Dim dt As New DataTable()
sda.Fill(dt)
mainMenu.DataSource = dt
mainMenu.DataBind()
End Using
End Using
End Using
End Sub

Answer

You need to use ListView ItemDataBound to bind your nested listview. ItemDatBound will get rows value for you. See the below code I done for you

<asp:ListView ID="mainMenu" runat="server" DataKeyNames="enter column name you want">
     <ItemTemplate>
          <li><asp:HyperLink ID="mainLinks" runat="server" NavigateUrl='<%# Eval("name", "~/{0}") %>' Text='<%# Eval("name") %>'></asp:HyperLink>
              <ul class="super-child">
                       <asp:ListView ID="childMenu" runat="server">
                            <ItemTemplate>
                                 <li><asp:HyperLink ID="cat3" runat="server" NavigateUrl='<%# Eval("category") & Eval("name", "/{0}") %>' Text='<%# Eval("name") %>'></asp:HyperLink></li>
                            </ItemTemplate>
                       </asp:ListView>
                </ul>
            </li>
      </ItemTemplate>
</asp:ListView> 

Protected Sub onItemDataBound(sender As Object, e As ListViewItemEventArgs)
        If e.Item.ItemType = ListViewItemType.DataItem Then
            Dim itm As ListViewDataItem = CType(e.Item, ListViewDataItem)
            Dim name As String = mainMenu.DataKeys(itm.DataItemIndex)("enter your datakeyname")
            Dim childMenu As ListView = TryCast(e.Item.FindControl("childMenu"), ListView)
            Dim constr As String = ConfigurationManager.ConnectionStrings("conio").ConnectionString
            Using con As New MySqlConnection(constr)
                Using cmd As New MySqlCommand()
                    cmd.CommandText = "SELECT * FROM tablename WHERE column = '" + name + "'"
                    cmd.Connection = con
                    Using sda As New MySqlDataAdapter(cmd)
                        Dim dt As New DataTable()
                        sda.Fill(dt)
                        childMenu.DataSource = dt
                        childMenu.DataBind()
                    End Using
                End Using
            End Using
        End If
    End Sub