overwrite menu class - hide menu item RRS feed

  • Question

  • User1796981909 posted
    hello, i derrived a new class from System.Web.UI.WebControls.Menu and i tried to override the method: protected override void OnMenuItemDataBound(MenuEventArgs e) { and there i want to hide a menu item like this: if (e.Item.Text == "myItem") { this.Items.Remove(e.Item); } base.OnMenuItemDataBound(e); } Everything compiles and seems to be okay. But the item is never deleted!!! what is wrong with my code or idea? how can i hide a certain menu-item?? as datasource i use a Web.sitemap file. thanks for help!!
    Tuesday, April 4, 2006 7:14 PM

All replies

  • User-1261216960 posted
    Try moving the base.OnMenuItemDataBound(e); line to the top of the method above your if statement.
    Tuesday, April 4, 2006 9:02 PM
  • User1796981909 posted
    thanks for reply... but it is not working either!! i have really no idea how to hide this item??!! it is absolutely necassary for me to thide it... any ideas?? need help. thx gonzo
    Wednesday, April 5, 2006 6:38 AM
  • User-1261216960 posted
    If all you are trying to do is hide a MenuItem, deriving from the Menu class to make a custom control is probably not the best route to take.

    All you have to do is set your Meny definition like this:

    <asp:Menu ID="Menu1" runat="server" DataSourceID="SiteMapDataSource1" OnMenuItemDataBound="Menu1_MenuItemDataBound"></asp:Menu>
    <asp:SiteMapDataSource ID="SiteMapDataSource1" runat="server" ShowStartingNode="False" />

    By the way, if all you want to do is hide the root node of the SiteMap, you just have to set the ShowStartingNode="False" on the SiteMapDataSource.  If you want to hide another node though, in the event handler you just set, you would do something like this:

    protected void Menu1_MenuItemDataBound(object sender, MenuEventArgs e)
        if (string.Equals(e.Item.DataPath, "/test/default.aspx", StringComparison.InvariantCultureIgnoreCase))

    That is basically the same logic that you have in your method.  It would be better to check aganist the Item.DataPath rather than the Text property since the DataPath is guaranteed to be unique for each MenuItem (it's the url if you use the SiteMapDataSource).  Also, make sure that your comparison is case-insensitive.  Maybe that is why your method is never removing the MenuItem.
    Wednesday, April 5, 2006 9:54 AM
  • User-481448394 posted

    Use Menu DataBound event,

    for e.g.

     protected void Menu1_DataBound(object sender, EventArgs e)

    //Locate the parent menu of the menuitem to be deleted.
    //You can use Menu1.FindItem to find the parent menu.

    Tuesday, May 16, 2006 2:43 AM
  • User1047905935 posted

    I am trying to do somthing similar but need to do things on 4 levels


     public class KBRMenu : Menu


            string s0ValuePath = null;
            string s1ValuePath = "";
            string s2ValuePath = "";
            string s3ValuePath = "";

            protected override void OnDataBound(EventArgs e)
                int iDepth = this.SelectedItem.Depth;

                if (iDepth == 3)
                    s0ValuePath = this.SelectedItem.Parent.Parent.Parent.ValuePath;
                    s1ValuePath = this.SelectedItem.Parent.Parent.ValuePath;
                    s2ValuePath = this.SelectedItem.Parent.ValuePath;
                    s3ValuePath = this.SelectedItem.ValuePath;
                else if (iDepth == 2)
                    s0ValuePath = this.SelectedItem.Parent.Parent.ValuePath;
                    s1ValuePath = this.SelectedItem.Parent.ValuePath;
                    s2ValuePath = this.SelectedItem.ValuePath;
                else if (iDepth == 1)
                    s0ValuePath = this.SelectedItem.Parent.ValuePath;
                    s1ValuePath = this.SelectedItem.ValuePath;
                else if (iDepth == 0)
                    s0ValuePath = this.SelectedItem.ValuePath;

                MenuItem mi0 = this.FindItem(s0ValuePath);
                MenuItem mi1 = this.FindItem(s1ValuePath);
                MenuItem mi2 = this.FindItem(s2ValuePath);
                MenuItem mi3 = this.FindItem(s2ValuePath);

                foreach (MenuItem mc0 in this.Items)//Level 0
                    if (mc0 != mi0)

                    foreach (MenuItem mc1 in mc0.ChildItems)//Level 1
                        if (mc1 != mi1)

                        foreach (MenuItem mc2 in mc1.ChildItems)//Level 2
                            if (mc2 != mi2)

                            foreach (MenuItem mc3 in mc2.ChildItems)//Level 3
                                if (mc3 != mi3)
                }//End foreachs

    Monday, December 4, 2006 6:12 PM
  • User-149903766 posted

    Another Solution:


          protected void Menu1_MenuItemDataBound(object sender, MenuEventArgs e)<?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" /><o:p></o:p>


                MenuItem menuItem = ((MenuItem)e.Item);<o:p></o:p>

                if (menuItem.Parent == null)<o:p></o:p>





    <o:p> </o:p>

    Thursday, March 15, 2007 1:09 PM
  • User-2137245748 posted

    thx groker...this is the solution [:)]


    Please can you explain to me the difference between Menu1.Items.Remove & menutem.Parent.ChildItems.Remove

    Monday, February 18, 2008 11:32 AM
  • User483264929 posted

    Thanks a lot for your solution....

    I search it two days.

    Thursday, May 21, 2009 3:26 AM