Lets say you have a Category Table in this format. and you want to show them as a Hierarchical Menu manually...
say you Table Looks like this...
Child Parent CategoryName
1 0 Home
2 1 About Us
3 1 Contact Us
4 3 via Telephone
And you want something like below...
Home > Contact Us > via Telephone
Then below is the Code for the menu.
Imports System.Data.SqlClient
Imports System.Data
Partial Public Class MenuTree
Inherits System.Web.UI.Page
Private _output As String
Private _childoutput As String
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
If Not Page.IsPostBack Then
Dim dt As DataTable
dt = Me.getSubs
Call Me.CreateTree(dt, 32)
Call Me.getChilds(dt, 19)
End If
End Sub
Private Function getSubs() As DataTable
Dim cmd As SqlDataAdapter
Dim con As SqlConnection
Dim dt As DataTable
con = New SqlConnection(util.ConString)
cmd = New SqlDataAdapter("Select * From CategoryRelationship", con)
Try
con.Open()
dt = New DataTable("Categories")
cmd.Fill(dt)
getSubs = dt
Catch ex As Exception
MsgBox(ex)
End Try
End Function
Private Sub CreateTree(ByVal dt As DataTable, ByVal iCatCode As Integer)
Dim CatCode As Integer
Dim Catparent As Integer
Dim Catname As String
For Each Item As DataRow In dt.Rows
CatCode = Item(0)
Catparent = Item(1)
Catname = Item(2)
If CInt(CatCode) = CInt(iCatCode) Then
_output = Catname & " > " & _output
Call Me.CreateTree(dt, Catparent)
End If
Next
showTree.Text = _output
End Sub
Private Sub getChilds(ByVal dt As DataTable, ByVal iCatcode As Integer)
Dim CatCode As Integer
Dim Catparent As Integer
Dim Catname As String
For Each Item As DataRow In dt.Rows
CatCode = Item(0)
Catparent = Item(1)
Catname = Item(2)
If CInt(Catparent) = CInt(iCatcode) Then
_childoutput = _childoutput & Catname & "
"
Call Me.getChilds(dt, CatCode)
End If
showChilds.Text = _childoutput
Next
End Sub
End Class