0

I am using a Radmenu. I like to hide a Submenu item.

The menu looks like following. Note that Car is the main Menu and Dodge, Toyota and Honda are submenus.

Cars - Dodge - Toyota - Honda

I am trying to do something like the following but won't work:

     protected void RadMenu1_ItemCreated(object sender, Telerik.Web.UI.RadMenuEventArgs e)
     {
      if (e.Item is RadMenuItem)
       {

        if (e.Item.Parent != null && e.Item.Parent is RadMenuItem)
        {
            if (e.Item.Menu.FindItemByText("Honda"))
            {
                e.Item.Visible = false;
            }
        }
    }
}
Nate Pet
  • 44,246
  • 124
  • 269
  • 414
  • Why don't you hide the item in Page_Load if the menu is defined declaratively in the html or after calling the DataBind() method, if the menu is databound? – Veselin Vasilev Mar 20 '15 at 06:13

2 Answers2

1

Please try with the below code snippet and let me know if any concern.

ASPX

<div>
    <telerik:RadScriptManager ID="RadScriptManager1" runat="server"></telerik:RadScriptManager>
    <telerik:RadMenu ID="RadMenu1" runat="server"  OnPreRender="RadMenu1_PreRender">
        <Items>
            <telerik:RadMenuItem Text="cars">
                <Items>
                    <telerik:RadMenuItem Text="Honda"></telerik:RadMenuItem>
                    <telerik:RadMenuItem Text="Toyota"></telerik:RadMenuItem>
                </Items>
            </telerik:RadMenuItem>
        </Items>
    </telerik:RadMenu>
</div>

ASPX.CS

protected void RadMenu1_PreRender(object sender, EventArgs e)
{
    foreach (RadMenuItem item in (sender as RadMenu).Items)
    {
        if (item.Items.Count > 0)
        {
            foreach (RadMenuItem citem in item.Items)
            {   
                if (citem.Text != "Honda")
                {
                    citem.Visible = false;
                }
            }
        }
    }
}

// Hide top menu cars
protected void RadMenu1_PreRender(object sender, EventArgs e)
{
foreach (RadMenuItem item in (sender as RadMenu).Items)
{
    if (item.Items.Count > 0)
    {
        if(item.Text == "cars")
        {
          item.Visible = false;
        }
        foreach (RadMenuItem citem in item.Items)
        {   
            if (citem.Text != "Honda")
            {
                citem.Visible = false;
            }
        }
    }
}
}
Jayesh Goyani
  • 11,008
  • 11
  • 30
  • 50
  • Hi Jayesh, can you assist me on: http://stackoverflow.com/questions/29216099/radgrid-on-edit-change-label-of-text-box – Nate Pet Mar 23 '15 at 17:13
  • Hi Jayesh, what is the best way to hide the top menu "cars". I have some top level menus in my code I need to hide. – Nate Pet Dec 09 '15 at 17:52
  • if only one item in top menu then I would suggest that please set 'RadMenu1.Visible="false"' because empty menu also created some 'div' element in page and if you have more then one menu on top level then you can only hide that option and I will also update my above post soon. – Jayesh Goyani Dec 09 '15 at 18:11
0

Eight years old, but I have a slightly different solution, based on the solution @Jayesh provided. Nothing in the ASPX page at all, since the event is handled in Page_Load.

ASPX.CS:

protected void Page_Load(object sender, EventArgs e)
{
    if(Page.IsPostBack)
    {
        return;
    }
    Telerik.Web.UI.RadMenu topLevel = RadMenu1;
    foreach(RadMenuItem item in topLevel.Items)
    {
        LookForRadMenuItems(item);
    }
}

private void LookForRadMenuItems(RadMenuItem sender)
{
    if(sender.Text == "Honda")
    {
        sender.Visible = false;
    }
    foreach(RadMenuItem item in sender.items)
    {
        LookForRadMenuItems(item);
    }
}

This will look for all instances of "Honda" in all menus and submenus, and hide them. You can make it more flexible, by checking if the text .contains "Honda" instead, for instance, or creating a dictionary or hashtable for looking for more than one item at a time.

My particular application is actually in VB.NET, so here is the code equivalent for that:

VB.NET

Protected Sub Page_Load(sender As Object, e As System.EventArgs)
    If Page.IsPostBack Then Exit Sub
    Dim topLevel As Telerik.Web.UI.RadMenu = RadMenu1
    For Each item As RadMenuItem In topLevel.Items
        LookForRadMenuItems(item)
    Next
End Sub

Private Sub LookForRadMenuItems(sender as RadMenuItem)
    If sender.Text = "Honda" Then
        sender.Visible = false
    End If
    For Each(item As RadMenuItem In sender.Items)
        LookForRadMenuItems(item)
    Next
End Sub
Michael Foster
  • 420
  • 6
  • 12