?
Solved

Programmically add sub menus to ASP.Menu control

Posted on 2014-04-28
9
Medium Priority
?
2,128 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
[X]
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
  • 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
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 

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

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

I recently went through the process of creating a Calendar Control of events with the basis of using a database to keep track of the dates that are selectable, one requirement was to have the selected date pop-up in a simple lightbox.  At first this…
Today is the age of broadband.  More and more people are going this route determined to experience the web and it’s multitude of services as quickly and painlessly as possible. Coupled with the move to broadband, people are experiencing the web via …
In this video we outline the Physical Segments view of NetCrunch network monitor. By following this brief how-to video, you will be able to learn how NetCrunch visualizes your network, how granular is the information collected, as well as where to f…
Do you want to know how to make a graph with Microsoft Access? First, create a query with the data for the chart. Then make a blank form and add a chart control. This video also shows how to change what data is displayed on the graph as well as form…

743 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