2013-05-08 3 views
1

아직 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 

답변

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 

를이 내가 그 이상으로 많은 도움하지만 그냥 생각하지 않아요 두려워 한 후 작동하지 않는 경우. 나는 당신의 문제가 메뉴를위한 또 하나의 프리로드를 필요로한다면 프리렌더를 사용하여 문제를 해결할 수있을 것이라고 생각한다.