2013-05-08 147 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 

如果這是行不通的話,只怕我沒有太大的幫助,除此之外,只是一個想法,但。我想,如果你的問題是你需要另一個預加載的菜單,然後使用預渲染也許可以解決它。