DeveloperBarn Forums

Go Back   DeveloperBarn Forums > Programming & Scripting > Code Samples > .Net

Discuss "VB.Net Dynamic Recursive Menu" in the .Net forum.

.Net - Post your ASP.net and Windows Forms code samples here.


Reply « Previous Thread | Next Thread »  
 
LinkBack (1) Thread Tools Display Modes
  1 links from elsewhere to this Post. Click to view. #1 (permalink)  
Old March 18th, 2008, 10:47 AM
jmurrayhead's Avatar
Your Lord & Master

 
Join Date: Mar 2008
Location: Reston, VA, USA
Posts: 543
Thanks: 14
Thanked 41 Times in 40 Posts
Blog Entries: 2
Rep Power: 1
jmurrayhead will become famous soon enough

Awards Showcase
Microsoft Windows Microsoft .Net Microsoft SQL Server Classic ASP 
Total Awards: 4

Default 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
Code:
Imports System.Data.OleDb
Partial Class _Menu
    Inherits System.Web.UI.Page
    '//Connection to database from web.config file
    Private conn As New OleDbConnection(ConfigurationManager.ConnectionStrings("menu").ConnectionString)
    Private Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) Handles MyBase.Load
        Call PopulateMenu()
    End Sub
    Private Sub PopulateMenu()
        Try
            '//Define new menu
            Dim menu As New Menu()
            '//Retrieve menu data
            Dim menuData As DataTable = GetMenuData()
            AddTopMenuItems(menuData, menu)
            Me.Panel1.Controls.Add(menu)
            Me.Panel1.DataBind()
        Catch ex As Exception
            Response.Write(ex.Message.ToString() & "<br />")
        End Try
    End Sub
    Private Function GetMenuData() As DataTable
        Try
            '//Populate DataTable
            Dim strSQL As String = "SELECT * FROM tbl_menu"
            Dim datMenu As OleDbDataAdapter = New OleDbDataAdapter(strSQL, conn)
            Dim tblMenu As DataTable = New DataTable()
            datMenu.Fill(tblMenu)
            Return tblMenu
        Catch ex As Exception
            Response.Write(ex.Message.ToString() & "<br />")
        End Try        
    End Function
    Private Sub AddTopMenuItems(ByVal menuData As DataTable, ByVal menu As Menu)
        Try
            '//Populate DataView
            Dim datView As DataView = New DataView(menuData)
            '//Filter parent menu items
            datView.RowFilter = "parentid = 0"
            '//Populate menu with top menu items
            Dim datRow As DataRowView
            For Each datRow In datView
                '//Define new menu item
                Dim parentMenu As MenuItem
                parentMenu = CreateMenuItem(datRow("linktext"), datRow("linkurl"), datRow("linktext"))
                menu.Items.Add(parentMenu)
                '//Populate child items of this parent
                AddChildMenuItems(menuData, datRow("itemid"), parentMenu)
            Next
        Catch ex As Exception
            Response.Write(ex.Message.ToString() & "<br />")
        End Try
    End Sub
    Private Sub AddChildMenuItems(ByVal menuData As DataTable, ByVal parentID As Integer, Byval parentMenu As MenuItem)
        Try
            '//Populate DataView
            Dim datView As DataView = New DataView(menuData)
 
            '//Filter child menu items
            datView.RowFilter = "parentid = " & parentID
            '//Populate parent menu item with child menu items
            Dim datRow As DataRowView
            For Each datRow in datView
                '//Define new menu item
                Dim childMenu As MenuItem
                childMenu = CreateMenuItem(datRow("linktext"), datRow("linkurl"), datRow("linktext"))
                parentMenu.ChildItems.Add(childMenu)
                '//Populate child items of this parent
                AddChildMenuItems(menuData, datRow("itemid"), childMenu)
            Next
        Catch ex As Exception
            Response.Write(ex.Message.ToString() & "<br />")
        End Try
    End Sub
    Private Function CreateMenuItem(ByVal strText As String, ByVal strUrl As String, ByVal strToolTip As String) As MenuItem
        Try
            '//Create new menu item
            Dim menuItem As New menuItem()
            '//Set properties of the menu item
            With menuItem
                .Text = strText
                .NavigateUrl = strUrl
                .ToolTip = strToolTip
            End With
            Return menuItem
        Catch ex As Exception
            Response.Write(ex.Message.ToString() & "<br />")
        End Try        
    End Function
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, 2 views)
File Type: zip treeview_vb.zip (1.2 KB, 1 views)
__________________
jmurrayhead
Did I help you out? Make me popular by clicking the icon!

If you found a post helpful, please click the button in the lower right-hand corner of the post.

Powered by ASP.Net
Reply With Quote
Sponsored Links
Reply

  DeveloperBarn Forums > Programming & Scripting > Code Samples > .Net

Bookmarks

Thread Tools
Display Modes

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

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On
Forum Jump

LinkBacks (?)
LinkBack to this Thread: http://www.developerbarn.com/net/27-visual-basic-net-dynamic-recursive-menu.html
Posted By For Type Date
Example code for treeview control populated from DB2 tables - ASP Free This thread Refback April 2nd, 2008 01:58 PM


Sponsored Links

ASP.NET Resource Index
a directory of ASP.NET tutorials, applications, scripts, assemblies and articles for the novice to professional developer.

Free Web Directory
Including Chats and Forums Resources, Offer automatic, instant and free directory submissions.
URLZ Web Directory
URLZ Web Directory

Free Web Directory - Add Your Link
The Little Web Directory
Free Web Directory
Pegasus free web directory is a free directory organised by categories.

Web Directory & SEO Services
dirroot web directory


All times are GMT -4. The time now is 12:44 AM.


Powered by vBulletin® Version 3.7.3
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO 3.2.0
Copyright © 2008 DeveloperBarn.com

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46