Code Bank
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
 
 
User Name:
Password:
Remember me
Go Back   ASP Free ForumsProgrammingCode Bank

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread ASP Free Forums Sponsor:
  #1  
Old November 28th, 2007, 12:19 PM
jmurrayhead's Avatar
jmurrayhead jmurrayhead is online now
The Drunken Moderator
ASP Free God 17th Plane (13000 - 13499 posts)
 
Join Date: Feb 2004
Location: Reston, VA, USA
Posts: 13,062 jmurrayhead User rank is General 9th Grade (Above 100000 Reputation Level)jmurrayhead User rank is General 9th Grade (Above 100000 Reputation Level)jmurrayhead User rank is General 9th Grade (Above 100000 Reputation Level)jmurrayhead User rank is General 9th Grade (Above 100000 Reputation Level)jmurrayhead User rank is General 9th Grade (Above 100000 Reputation Level)jmurrayhead User rank is General 9th Grade (Above 100000 Reputation Level)jmurrayhead User rank is General 9th Grade (Above 100000 Reputation Level)jmurrayhead User rank is General 9th Grade (Above 100000 Reputation Level)jmurrayhead User rank is General 9th Grade (Above 100000 Reputation Level)jmurrayhead User rank is General 9th Grade (Above 100000 Reputation Level)jmurrayhead User rank is General 9th Grade (Above 100000 Reputation Level)jmurrayhead User rank is General 9th Grade (Above 100000 Reputation Level)jmurrayhead User rank is General 9th Grade (Above 100000 Reputation Level)jmurrayhead User rank is General 9th Grade (Above 100000 Reputation Level)jmurrayhead User rank is General 9th Grade (Above 100000 Reputation Level)jmurrayhead User rank is General 9th Grade (Above 100000 Reputation Level)  Folding Points: 82293 Folding Title: Advanced FolderFolding Points: 82293 Folding Title: Advanced FolderFolding Points: 82293 Folding Title: Advanced FolderFolding Points: 82293 Folding Title: Advanced FolderFolding Points: 82293 Folding Title: Advanced Folder
Time spent in forums: 3 Months 6 Days 13 h 27 m 53 sec
Reputation Power: 1576
Facebook
VB.Net Dynamic Recursive Menu

I got this idea from helping another member with some similar code. This uses the Menu Class to build a menu a populates it from a database, allowing for multiple child menus. Sample files are included.

Menu.aspx
Code:
<%@ Page Language="VB" AutoEventWireup="false" CodeFile="menu.aspx.vb" Inherits="_Menu"%>
<html>
<head>
    <title>Recursive Menu Example</title>
</head>
    <body>
        <form id="Form1" runat="server">
            <asp:Panel ID="Panel1" runat="server" />
        </form>
    </body>
</html>


menu.aspx.vb
VB.Net Code:
Original - VB.Net Code
  1.  
  2. Imports System.Data.OleDb
  3.  
  4. Partial Class _Menu
  5.  
  6.     Inherits System.Web.UI.Page
  7.  
  8.     '//Connection to database from web.config file
  9.     Private conn As New OleDbConnection(ConfigurationManager.ConnectionStr  ings("menu").ConnectionString)
  10.  
  11.     Private Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) Handles MyBase.Load
  12.         Call PopulateMenu()
  13.     End Sub
  14.  
  15.     Private Sub PopulateMenu()
  16.         Try
  17.             '//Define new menu
  18.             Dim menu As New Menu()
  19.  
  20.             '//Retrieve menu data
  21.             Dim menuData As DataTable = GetMenuData()
  22.             AddTopMenuItems(menuData, menu)
  23.             Me.Panel1.Controls.Add(menu)
  24.             Me.Panel1.DataBind()
  25.         Catch ex As Exception
  26.             Response.Write(ex.Message.ToString() & "<br />")
  27.         End Try
  28.     End Sub
  29.  
  30.     Private Function GetMenuData() As DataTable
  31.         Try
  32.             '//Populate DataTable
  33.             Dim strSQL As String = "SELECT * FROM tbl_menu"
  34.             Dim datMenu As OleDbDataAdapter = New OleDbDataAdapter(strSQL, conn)
  35.             Dim tblMenu As DataTable = New DataTable()
  36.             datMenu.Fill(tblMenu)
  37.  
  38.             Return tblMenu
  39.         Catch ex As Exception
  40.             Response.Write(ex.Message.ToString() & "<br />")
  41.         End Try       
  42.     End Function
  43.  
  44.     Private Sub AddTopMenuItems(ByVal menuData As DataTable, ByVal menu As Menu)
  45.         Try
  46.             '//Populate DataView
  47.             Dim datView As DataView = New DataView(menuData)
  48.  
  49.             '//Filter parent menu items
  50.             datView.RowFilter = "parentid = 0"
  51.  
  52.             '//Populate menu with top menu items
  53.             Dim datRow As DataRowView
  54.             For Each datRow In datView
  55.                 '//Define new menu item
  56.                 Dim parentMenu As MenuItem
  57.                 parentMenu = CreateMenuItem(datRow("linktext"), datRow("linkurl"), datRow("linktext"))
  58.                 menu.Items.Add(parentMenu)
  59.  
  60.                 '//Populate child items of this parent
  61.                 AddChildMenuItems(menuData, datRow("itemid"), parentMenu)
  62.             Next
  63.         Catch ex As Exception
  64.             Response.Write(ex.Message.ToString() & "<br />")
  65.         End Try
  66.  
  67.     End Sub
  68.  
  69.     Private Sub AddChildMenuItems(ByVal menuData As DataTable, ByVal parentID As Integer, Byval parentMenu As MenuItem)
  70.         Try
  71.             '//Populate DataView
  72.             Dim datView As DataView = New DataView(menuData)
  73.        
  74.             '//Filter child menu items
  75.             datView.RowFilter = "parentid = " & parentID
  76.  
  77.             '//Populate parent menu item with child menu items
  78.             Dim datRow As DataRowView
  79.             For Each datRow in datView
  80.                 '//Define new menu item
  81.                 Dim childMenu As MenuItem
  82.                 childMenu = CreateMenuItem(datRow("linktext"), datRow("linkurl"), datRow("linktext"))
  83.                 parentMenu.ChildItems.Add(childMenu)
  84.  
  85.                 '//Populate child items of this parent
  86.                 AddChildMenuItems(menuData, datRow("itemid"), childMenu)
  87.             Next
  88.         Catch ex As Exception
  89.             Response.Write(ex.Message.ToString() & "<br />")
  90.         End Try
  91.     End Sub
  92.  
  93.     Private Function CreateMenuItem(ByVal strText As String, ByVal strUrl As String, ByVal strToolTip As String) As MenuItem
  94.         Try
  95.             '//Create new menu item
  96.             Dim menuItem As New menuItem()
  97.  
  98.             '//Set properties of the menu item
  99.             With menuItem
  100.                 .Text = strText
  101.                 .NavigateUrl = strUrl
  102.                 .ToolTip = strToolTip
  103.             End With
  104.  
  105.             Return menuItem
  106.         Catch ex As Exception
  107.             Response.Write(ex.Message.ToString() & "<br />")
  108.         End Try       
  109.     End Function
  110. End Class


Table design:

This setup uses a table with the following field types:
itemid - AutoNumber
parentid - Number
linktext - Text
linkurl - Text

Parent items will have parentid = 0 whereas child items will have parentid = itemid of the parent.
Attached Files
File Type: zip menu_vb.zip (8.8 KB, 127 views)
File Type: txt Default1.txt (4.2 KB, 78 views)
__________________
jmurrayhead

Did I help you out? Make me popular by clicking the icon!

New Members:Proper way to post a question

Powered by ASP.Net

Last edited by mehere : February 8th, 2008 at 09:25 AM.

Reply With Quote
Reply

Viewing: ASP Free ForumsProgrammingCode Bank > VB.Net Dynamic Recursive Menu


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump


Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 





© 2003-2008 by Developer Shed. All rights reserved. DS Cluster 5 hosted by Hostway