Solved

Programmically add sub menus to ASP.Menu control

Posted on 2014-04-28
9
1,869 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
 

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
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 

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

Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

Join & Write a Comment

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…
Creating an analog clock UserControl seems fairly straight forward.  It is, after all, essentially just a circle with several lines in it!  Two common approaches for rendering an analog clock typically involve either manually calculating points with…
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.
This video demonstrates how to create an example email signature rule for a department in a company using CodeTwo Exchange Rules. The signature will be inserted beneath users' latest emails in conversations and will be displayed in users' Sent Items…

760 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

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now