How to populate menuitems (parent and child) programmatically

J

Julia B

Hi

I've got a menu control that I want to populate programmatically but I can't
work out how to add parent and child menuitems. I've found loads of examples
of how to do it using datasets and xml files, but I don't want to do that. I
want to do it in vb code. A very simple example of what I want is this:

Dim parentItem As MenuItem
Dim childItem As MenuItem
parentItem = New MenuItem("Home", "Home", "", "~/Home.aspx")
Me.MenuApplication.Items.Add(parentItem)
parentItem = New MenuItem("Help", "Help", "", "~/Help.aspx")
Me.MenuApplication.Items.Add(parentItem)
If Session("CurrentUserRole") = "4" Then
parentItem = New MenuItem("Admin", "Admin", "", "")
Me.MenuApplication.Items.Add(parentItem)
'### this is where the problem is - how to make this a
child item??
childItem = New MenuItem("Users", "Users", "", "~/Users.aspx")
Me.MenuApplication.Items.Add(childItem)
childItem = New MenuItem("Accounts", "Accounts", "",
"~/Accounts.aspx")
Me.MenuApplication.Items.Add(childItem)
End If
etc.
 
J

Julia B

Worked it out as follows:

Dim parentItem As MenuItem
Dim childItem As MenuItem
parentItem = New MenuItem("Home", "Home", "", "~/Home.aspx")
Me.MenuApplication.Items.Add(parentItem)
parentItem = New MenuItem("Help", "Help", "", "~/Help.aspx")
Me.MenuApplication.Items.Add(parentItem)
If Session("CurrentUserRole") = "4" Then
parentItem = New MenuItem("Admin", "Admin", "", "")
Me.MenuApplication.Items.Add(parentItem)
childItem = New MenuItem("Users", "Users", "",
"~/Users.aspx")

Me.MenuApplication.FindItem("Admin").ChildItems.Add(childItem)
childItem = New MenuItem("Accounts", "Accounts", "",
"~/Accounts.aspx")

Me.MenuApplication.FindItem("Admin").ChildItems.Add(childItem)
End If
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,754
Messages
2,569,525
Members
44,997
Latest member
mileyka

Latest Threads

Top