从数据库填充菜单

时间:2017-11-17 18:42:11

标签: sql asp.net vb.net

我找到了一些非常有用的代码来填充数据库中的菜单并对其进行了一些定制,但我正在努力创建第三级菜单,如下例所示(2.1.1和2.1.2)

  1. 主页
  2. 关于我们 2.1管理团队 2.1.1团队成员1 2.1.2团队成员2 2.2公司信息
  3. 联系我们
  4. 我在下面列出了我的代码。

    Imports System.IO
    Imports System.Data
    Imports System.Data.SqlClient
    Imports System.Configuration
    Imports iconCloud_BL
    Partial Class Main
        Inherits System.Web.UI.MasterPage
        Public Params As New List(Of SqlParameter)
        Dim clsDatabase As New clsDatabaseLogic
        Protected Sub Page_Load(sender As Object, e As EventArgs)
            If Not Me.IsPostBack Then
    
                Dim dt As DataTable = Me.GetData(0, 2)
                PopulateMenu(dt, 0, Nothing)
    
    
            End If
        End Sub
    
        Private Function GetData(parentMenuId As Integer, role As Integer) As DataTable
            Dim query As String = "SELECT [menusMenuId], [menusTitle], [menusDescription], [menusUrl] FROM [configMenus] WHERE menusParentMenuId = @ParentMenuId AND menusRole = @Role"
            Dim constr As String = ConfigurationManager.ConnectionStrings("iconDataConnections").ConnectionString
            Using con As New SqlConnection(constr)
                Dim dt As New DataTable()
                Using cmd As New SqlCommand(query)
                    Using sda As New SqlDataAdapter()
    
                        cmd.Parameters.AddWithValue("@ParentMenuId", parentMenuId)
                        cmd.Parameters.AddWithValue("@Role", role)
                        cmd.CommandType = CommandType.Text
                        cmd.Connection = con
                        sda.SelectCommand = cmd
                        sda.Fill(dt)
                    End Using
                End Using
                Return dt
            End Using
        End Function
    
    
        Private Sub PopulateMenu(dt As DataTable, parentMenuId As Integer, parentMenuItem As MenuItem)
            Dim dtChild As DataTable
            Dim currentPage As String = Path.GetFileName(Request.Url.AbsolutePath)
            For Each row As DataRow In dt.Rows
                Dim rowcount = getMaxRows()
    
                Dim menuItem As New MenuItem() With {
                 .Value = row("menusMenuId").ToString(),
                 .Text = row("menusTitle").ToString(),
                  .NavigateUrl = row("menusUrl").ToString(),
                 .Selected = row("menusUrl").ToString().EndsWith(currentPage, StringComparison.CurrentCultureIgnoreCase)
                }
    
                    If parentMenuId = 0 Then
                        mainMenu.Items.Add(menuItem)
                        dtChild = Me.GetData(Integer.Parse(menuItem.Value), 2)
                        PopulateMenu(dtChild, Integer.Parse(menuItem.Value), menuItem)
                    ElseIf parentMenuId > 0 Then
    
                    For i As Integer = 0 To rowcount
                        mainMenu.Items.Add(menuItem)
                        dtChild = Me.GetData(Integer.Parse(i), 2)
                        PopulateMenu(dtChild, Integer.Parse(i), menuItem)
    
                    Next
                Else
                            parentMenuItem.ChildItems.Add(menuItem)
                    End If
                Next
    
        End Sub
    
        Public Function getMaxRows()
    
            clsDatabase.SQLCmd.CommandText = "sp_iconCloud_configMenuRowsCount"
            clsDatabase.SQLCmd.CommandType = CommandType.StoredProcedure
    
    
            clsDatabase.SQLCmd.Connection = clsDatabase.SQLConn
            clsDatabase.SQLConn.Open()
            Dim count As Integer = clsDatabase.SQLCmd.ExecuteScalar()
            clsDatabase.SQLConn.Close()
            clsDatabase.SQLCmd.Parameters.Clear()
    
    
            Return count
    
        End Function
    End Class
    

0 个答案:

没有答案