gpt4 book ai didi

javascript - 新手菜单/子菜单问题

转载 作者:行者123 更新时间:2023-11-28 09:09:59 24 4
gpt4 key购买 nike

我对 ASP.NET Web 编程还是新手,所以希望有人可以帮助我。我有一个水平主菜单和位于其正下方的另一个水平子菜单。

我将这些从数据库加载到 MenuCollection Session 变量中,该变量是 SubMenu 的字典及其 ParentId。

当用户单击主菜单项时,我想交换并显示正确的子菜单。

MainMenu.MenuItemClick 事件发生时,会发生回发,然后我尝试将正确的菜单从字典放入子菜单中,但它没有显示。

我是否需要另一个回发来加载子菜单或需要执行一些 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

最佳答案

是否可以在 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

如果这不起作用,那么恐怕除此之外我没有什么帮助,只是一个想法。我想如果您的问题是您需要再次预加载菜单,那么使用预渲染也许可以解决它。

关于javascript - 新手菜单/子菜单问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16448085/

24 4 0