Hey guys,
I use Visual Studio and am trying to create menu dynamically. So far, I've only been able to create the menu with the main items and the sub-item but I'm not able to create the sub-items of the sub-items. I will post the entire process here.
Code: MasterPage.master.vb
Imports System.Data
Imports System.Data.DataSet
Imports System.Linq
Imports System.IO
Imports System.Collections.Generic
Imports System.Web.UI
Imports System.Web.UI.Control
Partial Class menus_MasterPage
Inherits System.Web.UI.MasterPage
Private Menu As New DETI.objMenus
Private Sub menus_MasterPage_Load(sender As Object, e As EventArgs) Handles Me.Load
If Not IsPostBack Then
GerarMenu()
End If
End Sub
Private Sub MontaMenu(ByVal pTexto As String, ByVal pValor As Integer, ByVal pPagina As String)
Menu1.Items.Add(New MenuItem(pTexto, pValor, "", pPagina, ""))
End Sub
Private Sub MontaSubmenu(ByVal pItem As Integer, ByVal pTexto As String, ByVal pValor As Integer, ByVal pPagina As String, ByVal pTarget As String)
Menu1.Items.Item(pItem).ChildItems.Add(New MenuItem(pTexto, pValor, "", pPagina, pTarget))
End Sub
Private Sub GerarMenu()
Dim Ds, Ds2, Ds3 As Data.DataSet
Dim Dr, Dr2, Dr3 As Data.DataRow
Dim pID, pParente, pNivel As Integer
pID = 0
pParente = 0
pNivel = 0
Ds = Menu.Consultar("", "ID")
If Not Ds Is Nothing Then
If Ds.Tables(0).Rows.Count > 0 Then
' **************************************************************************
For I As Integer = 0 To Ds.Tables(0).Rows.Count - 1
With Ds
Dr = Ds.Tables(0).Rows(I)
pID = Dr("ID")
pParente = Dr("PARENTEID")
pNivel = Dr("NIVEL")
If pNivel = 0 Then MontaMenu(Dr("NOME"), Dr("ID"), "")
pID = Dr("ID")
Ds2 = Menu.Consultar(" A.PARENTEID = " & Dr("ID") & " AND A.NIVEL > 0 ", "")
If Not Ds2 Is Nothing Then
If Ds2.Tables(0).Rows.Count > 0 Then
For C As Integer = 0 To Ds2.Tables(0).Rows.Count - 1
With Ds2
Dr2 = Ds2.Tables(0).Rows(C)
pID = Dr2("ID")
pParente = Dr2("PARENTEID")
pNivel = Dr2("NIVEL")
If pNivel = 1 Then MontaSubmenu(C, Dr2("NOME"), pID, Dr2("ARQUIVO"), Dr2("TARGETMENU"))
Ds3 = Menu.Consultar(" A.PARENTEID = " & Dr2("ID") & " AND A.NIVEL = 2 ", "")
If Not Ds3 Is Nothing Then
If Ds3.Tables(0).Rows.Count > 0 Then
For D As Integer = 0 To Ds3.Tables(0).Rows.Count - 1
Dr3 = Ds3.Tables(0).Rows(D)
pID = Dr3("ID")
pParente = Dr3("PARENTEID")
pNivel = Dr3("NIVEL")
If pNivel = 2 Then MontaSubmenu(D, Dr3("NOME"), pID, Dr3("ARQUIVO"), Dr3("TARGETMENU"))
Next
End If
End If
End With
Next
End If
End If
End With
Next
' **************************************************************************
End If
End If
End Sub
End Class
The Menu is being created dynamically but the menu tree is being created like this
ASSISTANT
|_Localities
|_Countries
|_States
|_Cities
|_Axes
|_Areas
When should it be created like this:
ASSISTANT
|_Localities
- |_Countries
- |_States
- |_Cities
|_Axes
|_Area
That is, it creates the first levels but not the others. Could someone help me improve this model?
Grateful,
Ilano.