Link to home
Start Free TrialLog in
Avatar of Leo DiNicola
Leo DiNicolaFlag for United States of America

asked on

ASP.net programaticly add/delete menu items

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

Thank You,
Jim
Avatar of Paul MacDonald
Paul MacDonald
Flag of United States of America image

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.
Avatar of Leo DiNicola

ASKER

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"))
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)

...
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

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
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.
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
ASKER CERTIFIED SOLUTION
Avatar of Paul MacDonald
Paul MacDonald
Flag of United States of America image

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
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
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.