Link to home
Start Free TrialLog in
Avatar of pwashburn1224
pwashburn1224

asked on

Programmically add sub menus to ASP.Menu control

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
Avatar of Carl Tawn
Carl Tawn
Flag of United Kingdom of Great Britain and Northern Ireland image

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

Avatar of pwashburn1224
pwashburn1224

ASKER

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.
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:
User generated imageWhich is what I would expect it to do.
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
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.
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.
Do you not mean changed it from 1 to 2? Anyway, it's semantic, as long as it is working how you want it :)
ASKER CERTIFIED SOLUTION
Avatar of pwashburn1224
pwashburn1224

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
None of the replies suggested that I should look elsewhere as the code was correct.