2013-05-08 143 views
1

我还是新的ASP.NET Web编程,所以希望有人能帮助我。 我有一个水平的MainMenu和另一个水平的SubMenu正下方。新手菜单/子菜单的问题

我从数据库到MenuCollection Session变量是子菜单的字典加载这些和它的ParentId。

当用户点击我想交换中并显示正确的子菜单中的MainMenu项目。

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 

回答

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 

如果这是行不通的话,只怕我没有太大的帮助,除此之外,只是一个想法,但。我想,如果你的问题是你需要另一个预加载的菜单,然后使用预渲染也许可以解决它。