How to bind a drop down sidebar list to literal in VB.NET


The front-end page code is really just a literal control. This code here goes along with how to set up a sidebar drop-down list. Once you have your dropdown list working and also hiding in your responsive style. (which the code can be pulled from http://www.bootply.com) --- search for any off-canvas sidebar. This is VB.NET code for replacing the <ul> and <li> tags in code-behind.

 

'get top categories from db and put into datatable and build literal

Dim wholeLiteral As String = ""
Dim dtTopCats As New DataTable
dtTopCats = SQL2DataTable(sqlTopMenuCats)
If dtTopCats.Rows.Count > 0 Then

'get the submenus

For Each dr As DataRow In dtTopCats.Rows
     Dim litTop As String = "<li id='" & dr.Item("GroupGuid") & "' runat='server'><a href='#'><span>"      & dr.Item("Name") & "</span></a><ul>"
     Dim SqlSubMenu As String = "SELECT GroupGuid, [Name] FROM EECMCatalogCategory WHERE     ([Content] > 0) AND (Indent = 2) AND (ParentGuid = " & SafeString(dr.Item("GroupGuid")) &       ") ORDER BY SortIndex"
    Dim litMiddle As String = getMenu(SqlSubMenu, linkBase)
     Dim litBottom As String = "</ul></li>"
    wholeLiteral = wholeLiteral & litTop & litMiddle & litBottom
Next

End If
litMenu.Text = wholeLiteral


'End navigation list

 

'here is example of my getMenu function that builds the <li> tags

Protected Function getMenu(ByVal sql As String, ByVal linkBase As String) As String

Dim htmlMenu As String = ""
Dim htmlStart = "<li><a href='"
Dim dt As New DataTable
dt = SQL2DataTable(sql)
dt.Columns.Add("Link")
If dt.Rows.Count > 0 Then

For Each dr As DataRow In dt.Rows

Dim linktext As String = dr.Item("Name")
Dim GroupGuid As String = dr.Item("GroupGuid")
Dim htmlEnd As String = "'>" & linktext & "</a></li>"
Dim link As String = htmlStart & linkBase & GroupGuid & htmlEnd
dr.Item("Link") = link
htmlMenu = htmlMenu & link

Next

End If
Return htmlMenu

End Function

 

'For the sql2DataTable function (also see 'Server/SQL Connection methods) but for convenience:

Public Shared Function SQL2DataTable(ByVal sql As String) As DataTable

Dim da As SqlDataAdapter
Dim conn As SqlConnection = GetDBConnection()
Dim cmd As New SqlCommand(sql, conn)
cmd.CommandTimeout = 0 'Don't time out
Dim dt As New DataTable
da = New SqlDataAdapter(cmd)
da.Fill(dt)
da.Dispose()
conn.Close()
Return dt

End Function

'getting a db connection

Public Shared Function GetDBConnection() As SqlConnection

Dim conn As New SqlConnection(ConnStr)
conn.Open()
Return conn

End Function

Last update:
2014-08-18 14:36
Author:
Mark
Revision:
1.1
Average rating:0 (0 Votes)

You can comment this FAQ

Chuck Norris has counted to infinity. Twice.

Records in this category

Tags