아직 ASP.NET 웹 프로그래밍에 익숙하지 않으므로 누군가 나를 도와 줄 수 있기를 바랍니다. 필자는 수평 MainMenu와 수평 하부 메뉴를 직접 가지고 있습니다.초보자 메뉴/하위 메뉴 문제
데이터베이스에서 SubMenu의 Dictionary이고 ParentId 인 MenuCollection 세션 변수에로드하고 있습니다.
사용자가 MainMenu 항목을 클릭하면 바꿔 넣기를하고 올바른 하위 메뉴를 표시하려고합니다.
MainMenu.MenuItemClick
이벤트가 발생하면 다시 게시가 발생하고 사전에서 올바른 메뉴를 하위 메뉴에 넣으려고하지만 표시되지 않습니다.
하위 메뉴를로드하거나 자바 스크립트를 수행해야하는 다른 포스트 백이 필요합니까? 아니면 잘못된 방향으로 가고 있습니까?
아래 코드는 제 코드입니다. 감사.
Imports System.Data
Imports System.Data.SqlClient
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, Menu)
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())
newMenuItem.NavigateUrl = row("NavigateUrl").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 Menu
Dim view As DataView = Nothing
Dim Result As New Menu
view = New DataView(menuData)
view.RowFilter = "ParentId=" & parentMenuItem.Value
For Each row As DataRowView In view
If row("IsActive") Then
Dim newMenuItem As New MenuItem(row("Text").ToString(), row("Id").ToString())
newMenuItem.NavigateUrl = row("NavigateUrl").ToString()
Result.Items.Add(newMenuItem)
End If
Next
Return Result
End Function
Protected Sub MainMenu_ItemClick(source As Object, e As MenuEventArgs) Handles MainMenu.MenuItemClick
If Not Session("MenuCollection") Is Nothing Then
Dim MenuDictionary As Dictionary(Of Integer, Menu) = DirectCast(Session("MenuCollection"), Dictionary(Of Integer, Menu))
If MenuDictionary.ContainsKey(e.Item.Value) Then
SubMenu = MenuDictionary.Item(e.Item.Value)
End If
End If
End Sub
End Class