create menu with submenu using asp.net vb and entity framework

1.1k Views Asked by At

i trying to create menu with sub menu using repeater and entity data model in asp.net (vb)

enter image description here

page_appear: means display in menu or not

 <asp:Repeater ID="menuRepeater" runat="server" DataSourceID="EntityDataSource2">
          <headertemplate>
                        <div class="menu"><ul>
                    </headertemplate>
                    <itemtemplate>
                        <li>
                            <a href='#'><%# Eval("page_name")%></a>
                            <asp:literal ID="ltrlSubMenu" 
                            runat="server"></asp:literal>
                        </li>
                    </itemtemplate>
                <footertemplate>
                       </ul></div>
                </footertemplate>

    </asp:Repeater>

`<asp:EntityDataSource ID="EntityDataSource2" runat="server" ConnectionString="name=AGIP_dbEntities" DefaultContainerName="AGIP_dbEntities" EnableFlattening="False" EntitySetName="tbl_pages" Select="it.page_name,it.page_parentID,it.page_active,it.page_appear" Where="it.page_parentID=0 and it.page_appear=TRUE and it.page_active=TRUE"`>

i don't have any idea how to create that

can anyonehelp me ?

thanks in advance

2

There are 2 best solutions below

0
Boney On

For Menu's it is usually a repeater inside repeater. Parent repeater for the main menu and the child repeater for the submenu.

In the ItemDataBound event of the Parent repeater, we bind the Child repeater based on the value of the MainMenu item(that we get from e As RepeaterItemEventArgs).

Please refer to the below link:
https://www.aspsnippets.com/Articles/Implement-Nested-Repeater-Repeater-inside-Repeater-with-example-in-ASPNet-using-C-and-VBNet.aspx

0
Mutaz Obeidat On

this is good idea to create dynamic menu

aspx

<ul id="menu" class="menu">
    <asp:Literal ID="litMainMenu" runat="server"></asp:Literal>
</ul>

vb

Protected Sub Page_Load(sender As Object, e As EventArgs) Handles Me.Load
    Dim objStringBuilder As New StringBuilder
    If Not IsPostBack Then
        Using context As New AGIP_dbModel.AGIP_dbEntities()
            Dim obj = context.tbl_pages.Where(Function(u) u.page_parentID = 0 And u.page_appear = True)

            objStringBuilder.AppendLine("<ul>")
            For Each objPage In obj.ToList

                objStringBuilder.AppendLine("<li><a href=" & objPage.page_url & ".aspx>" & objPage.page_name & "</a>")

                If obj.ToList.Count > 0 Then
                    Dim objsu = context.tbl_pages.Where(Function(u) u.page_parentID = objPage.page_id And u.page_appear = True)

                    objStringBuilder.AppendLine("<ul>")
                    For Each objSub In objsu.ToList
                        objStringBuilder.AppendLine("<li><a href=" & objSub.page_url & ">" & objSub.page_name & "</a>")

                    Next
                    objStringBuilder.AppendLine("</li>")
                    objStringBuilder.AppendLine("</ul>")
                Else
                    objStringBuilder.AppendLine("</li>")
                    objStringBuilder.AppendLine("</ul>")
                End If


            Next
        End Using

    End If
    litMainMenu.Text = objStringBuilder.ToString
End Sub