S Nash
S Nash

Reputation: 2509

Removing menus from ASP.NET menu

I'm working on somebody code which left.

This is an ASP.NET WebForm App. For authorizations many roles are defined.

He defines a Menu infrastructure on a master page with no MenuItems.

 <asp:SiteMapDataSource ID="SiteMapDataSource1" runat="server" />
                            <asp:Menu ID="Menu1" CssClass="noflicker" runat="server"
                                Orientation="Horizontal" StaticDisplayLevels="2" OnMenuItemClick="Menu1_MenuItemClick" DataSourceID="SiteMapDataSource1">
                                <StaticMenuStyle BorderStyle="None" />
                                <StaticSelectedStyle BackColor="#1e1a53" ForeColor="white" Height="25px" Font-Size="18px" Font-Names="Arial" />
                                <StaticMenuItemStyle HorizontalPadding="14px" BorderStyle="None" BorderWidth="1px" Font-Size="18px" ForeColor="White" Height="25px" Font-Bold="False" Font-Names="Arial" />
                                <StaticHoverStyle Height="25px" Font-Names="Arial" ForeColor="white" BackColor="#F99D1C" BorderColor="#3366CC" />
                                <DynamicHoverStyle Height="35px" Font-Names="Arial" ForeColor="white" BackColor="#F99D1C" Font-Size="18px" />
                                <DynamicMenuStyle BorderStyle="Solid" BorderColor="#1e1a53" BorderWidth="1px" Height="25px"
                                    Font-Names="Arial" ForeColor="White" BackColor="#1e1a53" VerticalPadding="3px" />
                                <DynamicSelectedStyle Height="35px" Font-Names="Arial" BorderColor="#1e1a53" BorderWidth="1px" ForeColor="White" BackColor="#1e1a53" />
                                <DynamicMenuItemStyle BorderStyle="Solid" BorderWidth="1px" BorderColor="#1e1a53" Height="35px"
                                    ForeColor="White" BackColor="#1e1a53" HorizontalPadding="3px" Font-Bold="False" Font-Size="18px"
                                    Font-Names="Arial" CssClass="leftMenuItem" VerticalPadding="5px" ItemSpacing="3px" />
                                <DataBindings>
                                    <asp:MenuItemBinding DataMember="SiteMapNode" TextField="Title" ToolTipField="description" />
                                </DataBindings>
                            </asp:Menu>

The menu gets populated in runtime using sitemap:

 SiteMapDataSource1.SiteMapProvider = "allMenuItemsSiteMap";

I know if Menu was defined using Menuitems it was easy to Remove those.

However, using sitemap is there any way to remove an item on the menu?

Currently, the only way I know is to define different sitemaps for different roles, which is definitely not a good solution,

Upvotes: 0

Views: 584

Answers (1)

Kevin
Kevin

Reputation: 2631

Here is a C# function, should be easy to convert to vb.net. You would need to change it to be recursive if you have more than two level deep menus.

private void RemoveMenuItem(string itemText, Menu menu)
{
   foreach (MenuItem menuItem in menu.Items)
   {
      if (menuItem.Text.Equals(itemText, StringComparison.CurrentCultureIgnoreCase))
      {
           menu.Items.Remove(menuItem);
           break;
      }

      bool found = false;

      if (menuItem.ChildItems.Count > 0) RemoveChildMenuItem(itemText, menuItem, out found);

      if (found) break;
    }
}

private void RemoveChildMenuItem(string itemText, MenuItem mi, out bool Found)
{
      Found = false;

      foreach(MenuItem menuItem in mi.ChildItems)
      {
         if (menuItem.Text.Equals(itemText, StringComparison.CurrentCultureIgnoreCase))
        {
            mi.ChildItems.Remove(menuItem);
            Found = true;
            break;
        }                
     }
}

Upvotes: 1

Related Questions