Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
Solved

Programmically add sub menus to ASP.Menu control

Posted on 2014-04-28
9
2,000 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
Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 

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

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

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

It’s quite interesting for me as I worked with Excel using vb.net for some time. Here are some topics which I know want to share with others whom this might help. First of all if you are working with Excel then you need to Download the Following …
Parsing a CSV file is a task that we are confronted with regularly, and although there are a vast number of means to do this, as a newbie, the field can be confusing and the tools can seem complex. A simple solution to parsing a customized CSV fi…
Email security requires an ever evolving service that stays up to date with counter-evolving threats. The Email Laundry perform Research and Development to ensure their email security service evolves faster than cyber criminals. We apply our Threat…
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …

839 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