Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Swap in a submenu based on a main menu click

Status
Not open for further replies.

michael0914

Programmer
May 7, 2013
1
US
I am still new to VB and ASP.NET so hopefully someone can help me out.
I have a horizontal MainMenu and another horizontal SubMenu directly under it.

I am loading these from a database into a MenuCollection Session variable which is a Dictionary of the SubMenu and it's ParentId.

When the user clicks a MainMenu item I want to swap in and display the correct SubMenu.

When the `MainMenu.ItemClick` event happens the postback occurs and then I try to put the correct menu from the Dictionary into the SubMenu but it doesn't show.

Do I need another postback for the SubMenu to load or need to do some javascript?
Or am I going about this the wrong way?

Also, am I using the Session variables correctly to store the MenuCollection and MenuData
so I can use them later?

Below is my code.
The "ASPxMenu"s are DevExpress controls.

Thanks.

Code:
    Imports System.Data    
    Imports System.Data.SqlClient    
    Imports DevExpress.Web.ASPxMenu    
    Imports System.Collections.Generic    

    Public Class RootMaster Inherits System.Web.UI.MasterPage

    Private ReadOnly connection As String = System.Configuration.ConfigurationManager.ConnectionStrings("ConnectionString").ConnectionString

        Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
            If Not IsPostBack Then
                Session("MenuData") = GetMenuData()
                AddTopMenuItems(Session("MenuData"))
            End If
        End Sub

        Private Function GetMenuData() As DataTable
            Using con As New SqlConnection(connection)
                Dim cmd As New SqlCommand("Select * from MenuData", con)
                Dim dtMenuItems As New DataTable()
                Dim sda As New SqlDataAdapter(cmd)

                sda.Fill(dtMenuItems)
                cmd.Dispose()
                sda.Dispose()

                Return dtMenuItems
            End Using
        End Function

        Private Sub AddTopMenuItems(menuData As DataTable)
            Dim view As DataView = Nothing
            Dim MenuDictionary As New Dictionary(Of Integer, ASPxMenu)

            view = New DataView(menuData)
            view.RowFilter = "ParentId IS NULL"
            For Each row As DataRowView In view
                'Adding the menu item
                If row("IsActive") Then
                    Dim RowId As Integer = row("Id")
                    Dim newMenuItem As New MenuItem(row("Text").ToString(), RowId.ToString())
                    MainMenu.Items.Add(newMenuItem)
                    'Create all sub menus for each main menu item, add to dictionary
                    Dim SubM = CreateSubMenus(menuData, newMenuItem)
                    If SubM.Items.Count > 0 Then
                        MenuDictionary.Add(RowId, SubM)
                    End If
               End If
            Next

            Session("MenuCollection") = MenuDictionary
            MainMenu.Items(0).Selected = True
            view = Nothing
        End Sub

        Private Function CreateSubMenus(menuData As DataTable, parentMenuItem As MenuItem) As ASPxMenu
            Dim view As DataView = Nothing
            Dim Result As New ASPxMenu

            view = New DataView(menuData)
            view.RowFilter = "ParentId=" & parentMenuItem.Name

            For Each row As DataRowView In view
                If row("IsActive") Then
                     Dim newMenuItem As New MenuItem(row("Text").ToString(), row("Id").ToString())
                     Result.Items.Add(newMenuItem)
                End If
            Next

            Return Result
        End Function

        Protected Sub MainMenu_ItemClick(source As Object, e As DevExpress.Web.ASPxMenu.MenuItemEventArgs) Handles MainMenu.ItemClick
            If Not Session("MenuCollection") Is Nothing Then
                Dim MenuDictionary As Dictionary(Of Integer, ASPxMenu) =  DirectCast(Session("MenuCollection"), Dictionary(Of Integer, ASPxMenu))
  
                If MenuDictionary.ContainsKey(e.Item.Name) Then
                    SubMenu = MenuDictionary.Item(e.Item.Name)
                End If
            End If
        End Sub
    End Class
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top