IT researcher IT researcher - 6 months ago 23
Vb.net Question

Remove a child menu item

I have a asp menu as show below. I want to hide the sub item having text "Old" from code behind in one of the case.

<asp:Menu ID="Viewmenu" runat="server" Visible="true" PathSeparator="," BackColor="#FFFFEB" DynamicHorizontalOffset="2" >

<Items>
<asp:MenuItem Text="View" Value="New Item">
<asp:MenuItem Text="Old" Value="Old que"></asp:MenuItem>
<asp:MenuItem Text="Profile" Value="Address"></asp:MenuItem>
</asp:MenuItem>
</Items>
</asp:Menu>


So i used finditem and then item remove from code behind as shown below

Dim s As MenuItem
s = Viewmenu.FindItem("New Item,Old que")
Viewmenu.Items.Remove(s)


But above code run without any error, but the menu item having Text="Old" is not removed and is shown in menu.(in my case I am trying to remove a sub menuitem from a menuitem)
I tried using
Viewmenu.FindItem("New Item")
just to check whether code works and complete menu itself removed as expected.

So why does remove a menu item inside a menuitem didn't work? What I am missing? OR is there any better way to achieve the same from code behind?

Answer

Try below code,hope it helps,and you used C# tag,but I guess your code is VB

C# Code

MenuItem parent = Viewmenu.FindItem("New Item"); 
MenuItem s = Viewmenu.FindItem("@New Item\Old que");
parent.ChildItems.Remove(s);

VB Code

Dim parent As MenuItem = Viewmenu.FindItem("New Item")
Dim s As MenuItem = Viewmenu.FindItem("New Item,Old que")
parent.ChildItems.Remove(s)
Comments