Solved

Programmically add sub menus to ASP.Menu control

Posted on 2014-04-28
9
1,974 Views
Last Modified: 2014-05-07
Trying to programmatically add a Menu item to an asp:Menu control and then Submenus to that added Menu item.   No matter what I try, the Submenu item is added to the main menu, not as a submenu of the Menu item I've added.   Menu1 is a asp:Menu control on the aspx page.  mi is a new parent level menu item.  ci is a child item that should be added to the mi parent level menu item.   But it's ending up at the parent level, not the child of mi.  

Here's the code that doesn't seem to work:

    Protected Sub Page_Load(sender As Object, e As System.EventArgs) Handles Me.Load
        If IsPostBack = False Then
            Dim mi As MenuItem
            Dim ci As MenuItem

            mi = New MenuItem
            mi.Text = "MenuItem"
            mi.ToolTip = "MenuItem"
            mi.Value = "MenuItem"
            Menu1.Items.Add(mi)

            ci = New MenuItem
            ci.Text = "SubMenu"
            ci.ToolTip = "SubMenu"
            ci.Value = "SubMenu"
            ci.NavigateUrl = ""
            Menu1.FindItem("MenuItem").ChildItems.Add(ci)
        End If
    End Sub
0
Comment
Question by:pwashburn1224
  • 6
  • 3
9 Comments
 
LVL 52

Expert Comment

by:Carl Tawn
ID: 40027413
Instead of:
Menu1.FindItem("MenuItem").ChildItems.Add(ci)

Open in new window

You should be able to add your child item directly to the parent reference:
mi.ChildItems.Add(ci)

Open in new window

0
 

Author Comment

by:pwashburn1224
ID: 40029632
No change to the problem.  "SubMenu" shows up on the top level menu next to "MenuItem".  

I think
Menu1.FindItem("MenuItem") and
mi would point to the same object.

Is there some property of Menu1 that I haven't set properly maybe?

Am I pointing to the right .Net framework in my IDE?  I'm using VWD 2010.

Driving me crazy.  All the references that I can find indicate this is the correct sample code to add a child menu to a parent menu.

Am I using the right terminology?

When "MenuItem" is clicked, I want a submenu to come up with "Submenu" on it.
I'm on the right track aren't I.
0
 
LVL 52

Expert Comment

by:Carl Tawn
ID: 40029675
What you have looks right. I tested with the following:
// markup
<asp:Menu ID="Menu1" runat="server">
</asp:Menu>

'// Code behind
Menu1.Items.Add(New MenuItem("Top-level"))
Menu1.FindItem("Top-level").ChildItems.Add(New MenuItem("Child1"))

Open in new window

Which resulted in:
MenuWhich is what I would expect it to do.
0
Ransomware: The New Cyber Threat & How to Stop It

This infographic explains ransomware, type of malware that blocks access to your files or your systems and holds them hostage until a ransom is paid. It also examines the different types of ransomware and explains what you can do to thwart this sinister online threat.  

 

Author Comment

by:pwashburn1224
ID: 40029707
Digging a little deeper, I don't think it's my coding that is the problem.  I think it is working okay.  I think it's the way the menu is being presented is the problem.

I want the menu to be displayed horizontally as follows:
Menu1      Menu2     Menu3     Menu4

When the mouse pointer hovers over one of the main menu items, I want the submenu to pop up.   The attached file shows the type of menu I'm looking for.
Screen-Shot-2014-04-29-at-10.51..png
0
 

Author Comment

by:pwashburn1224
ID: 40029729
Thanks Carl,

That's what I'm getting too.   The behavior I want is for Top Level only to be displayed.   When the mouse hovers over Top Level, then Child1 pops up.  So I have the wrong style for the menu picked out or maybe I'm using the wrong type of control.
0
 

Author Comment

by:pwashburn1224
ID: 40029770
Think I found the issue:
StaticDisplayLevels property

I had it set to 2
Setting it to 1 gives me the behavior I'm looking for.
0
 
LVL 52

Expert Comment

by:Carl Tawn
ID: 40029809
Do you not mean changed it from 1 to 2? Anyway, it's semantic, as long as it is working how you want it :)
0
 

Accepted Solution

by:
pwashburn1224 earned 0 total points
ID: 40037488
The StaticDisplayLevels property was 2 when I had my issues.  I'm a novice at .ASPX Menus controls but I think by setting it at 2, both my MenuItem and the SubMenu were being treated as Static menus and were displayed continuously.  When I changed it to 1, only MenuItem was considered 'static' and displayed continiously.  SubMenu was then 'dynamic' and only popped down when the mouse hovered over MenuItem.
0
 

Author Closing Comment

by:pwashburn1224
ID: 40046642
None of the replies suggested that I should look elsewhere as the code was correct.
0

Featured Post

Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In an ASP.NET application, I faced some technical problems. In this article, I list them out and show the solutions that I found.  I hope it will be useful. Problem: After closing a pop-up window, the parent page should be refreshed automaticall…
ASP.Net to Oracle Connectivity Recently I had to develop an ASP.NET application connecting to an Oracle database.As I am doing it first time ,I had to solve several problems. This article will help to such developers  to develop an ASP.NET client…
This Micro Tutorial demonstrates using Microsoft Excel pivot tables, how to reverse engineer competitors' marketing strategies through backlinks.
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

809 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question