We help IT Professionals succeed at work.

ASP.net programaticly add/delete menu items

Leo DiNicola
Leo DiNicola asked
on
I would like to add or delete menu items programaticly.  Is this possible and if Yes then how.

Thank You,
Jim
Comment
Watch Question

Paul MacDonaldDirector, Information Systems
CERTIFIED EXPERT

Commented:
It's probably possible, but you'll need to give us a clue as to how you're generating your menu and under what conditions you want it to change.

Author

Commented:
This is in site.Master:
           <asp:Menu ID="NavigationMenu" runat="server" CssClass="menu" EnableViewState="false" IncludeStyleBlock="false" Orientation="Horizontal">
                    <Items>
                        <asp:MenuItem NavigateUrl="~/Default.aspx" Text="Home"/>
                        <asp:MenuItem NavigateUrl="~/Products.aspx" Text="Products"/>            
                        <asp:MenuItem NavigateUrl="~/Contact.aspx" Text="Contact Us"/>
                        <asp:MenuItem NavigateUrl="~/News.aspx" Text="News"/>
                        <asp:MenuItem NavigateUrl="~/About.aspx" Text="About Us"/>
                    </Items>
            </asp:Menu>

and this is the beginning of what I have in code
               Dim Menu1 As WebControls.Menu = Master.FindControl("NavigationMenu")
                Menu1.Items.Add(New MenuItem("TEST"))
Paul MacDonaldDirector, Information Systems
CERTIFIED EXPERT

Commented:
Try:

...
    Dim Menu1 As WebControls.Menu = Master.FindControl("NavigationMenu")
   Dim mnuItem As New WebControls.MenuItem
    mnuItem.NavigateUrl = "~/somewhere.aspx"
    mnuItem.Text = "Test"
    Menu1.Items.Add(mnuItem)

...
nishant joshiTechnology Development Consultant
CERTIFIED EXPERT

Commented:
check code

Regards,
nishant
//c# code for remove item: you can simply convert it to vb
protected void Button1_Click(object sender, EventArgs e)
{
//your code
Menu1.Items.RemoveAt(0);
}

your asp code:

    <asp:Menu ID="Menu1" runat="server" Height="192px">
            <DynamicItemTemplate>
                <%# Eval("Text") %>
            </DynamicItemTemplate>
            <Items>
                <asp:MenuItem Text="1" Value="1"></asp:MenuItem>
                <asp:MenuItem Text="2" Value="2"></asp:MenuItem>
                 <asp:MenuItem Text="3" Value="3"></asp:MenuItem>
                 <asp:MenuItem Text="4" Value="4"></asp:MenuItem>
            </Items>
        </asp:Menu>

Open in new window

Author

Commented:
Hello Experts, thanks for the comments.  I come from a classic VB Windows Forms and can add/subtract menu items in that sense, similar to what you guys did.  My problem comes where I have to take the modified MENU1 and get it to point back to menu declared in the Site.Master file.  It seems to me that Menu1 is just a copy of the menu that the website is using.  This is where my classic VB.net ends and ASP.net understanding ends.

Thanks,
Jim
Paul MacDonaldDirector, Information Systems
CERTIFIED EXPERT

Commented:
You're talking about making the changes permanent?  If so, I think your best bet is to create a menu that includes all possible items, then mask/hide items based on whatever criteria you need to use.  
All that can be done inline in the master page.

Here's a little more information on using navigation with master pages:
http://www.asp.net/data-access/tutorials/master-pages-and-site-navigation-cs

If I'm not understanding, please let me know.

Author

Commented:
Hi Paul,
I was looking at a different page on that site but to no avail.  

The changes will happen based on who is logged in.  Some menu items will dissappear and some will need to reappear.  Again, my block is how do I dissable the menu items in my VB code (that's where I handle the login) based on the menu created in my Site.Master.

Thanks
Jim
Director, Information Systems
CERTIFIED EXPERT
Commented:
If you have a simple way of differentiating users - especially if you can do it by group, AD or otherwise - why not have the various menu items hide based on who's logged in.

In other words, somethng like:

<asp:Menu ID="NavigationMenu" runat="server" CssClass="menu" EnableViewState="false" IncludeStyleBlock="false" Orientation="Horizontal">
                    <Items>
                       <asp:MenuItem NavigateUrl="~/Default.aspx" Text="Home"/>
                       <asp:MenuItem NavigateUrl="~/Products.aspx" Text="Products"/>            
                       <% If Session("Group") = "Blue" Then %>
                          <asp:MenuItem NavigateUrl="~/BlueUsers.aspx" Text="Blue Users"/>            
                        <% End If %>
                       <asp:MenuItem NavigateUrl="~/Contact.aspx" Text="Contact Us"/>
                       <asp:MenuItem NavigateUrl="~/News.aspx" Text="News"/>
                       <% If Session("Group") = "Green" Then %>
                          <asp:MenuItem NavigateUrl="~/GreenUsers.aspx" Text="Green Users"/>            
                       <% End If %>
                       <asp:MenuItem NavigateUrl="~/About.aspx" Text="About Us"/>
                        <% If Session("Group") = "Green" or Session("Group") = "Orange" Then %>
                          <asp:MenuItem NavigateUrl="~/GreenOrangeUsers.aspx" Text="Green and Orange Users"/>            
                       <% End If %>
                   </Items>
            </asp:Menu>


So you're simply selectively hiding menu items when people shouldn't be able to see them.

Author

Commented:
We're going to go a different route.  I would prefer to stay away from Session variables.  This whole SiteMap issue and Menu items being enabled/disabled has been very time-consuming and frustrating for a couple of us.  We would have thought this would be simpler.

Thanks for your efforts.
Jim
Paul MacDonaldDirector, Information Systems
CERTIFIED EXPERT

Commented:
I only used session variables as an example.  I do something similar on my web site to hide an administrative section which in turn has two separate areas for our office folk and our support folk.  The process is effectively the same but I make calls to check for AD group membership each time I render my menu.  I use a simple CSS menu though, eschewing the SiteMap control.

There are a couple other options I can think of, but none of them I'd call simple.  If you care to share, I'd like to hear what you're considering for a solution - if only because it may help someone else at some point.