我仍然是ASP.NET网络编程的新手,所以希望有人可以帮助我。 我有一个水平的MainMenu和另一个水平的SubMenu。
我将这些从数据库加载到MenuCollection Session变量中,该变量是SubMenu的字典,它是ParentId。
当用户点击我要交换的MainMenu项目并显示正确的SubMenu时。
当MainMenu.MenuItemClick
事件发生时,回发发生,然后我尝试将正确的菜单从字典放入SubMenu,但它没有显示。
我是否需要为SubMenu加载或需要执行一些javascript的其他回发? 或者我是以错误的方式解决这个问题?
以下是我的代码。感谢。
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
答案 0 :(得分:0)
是否可以在PreRender事件上重绘子菜单。所以,例如:
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
可能会成为:
Protected Sub MainMenu_PreRender(ByVal sender As Object, ByVal e As EventArgs) Handles MainMenu.PreRender
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
如果这不起作用那么我恐怕除此之外我没多大帮助,只是想一想。我想如果你的问题是你需要为菜单另外预加载,那么使用预渲染可能可以解决它。