This is a migrated thread and some comments may be shown as answers.

Dynamically modify RadTreeNode's ContextMenu?

4 Answers 185 Views
TreeView
This is a migrated thread and some comments may be shown as answers.
Nick
Top achievements
Rank 1
Nick asked on 17 Oct 2008, 02:00 PM
Hi,

I have a RadTreeView which has a context menu defined in the aspx page.  The nodes are loaded from an xml file.

Is there any way I can enable/disable context menu items dependant on the attributes of each individual treenode?  I.E. some of the nodes will be marked as uneditable so I would like to disable that item in that node's context menu.

XML Source:
<Tree> 
  <Node Text="Front Page" Value="" ContextMenuID="rtvcmFrontPage"/> 
  <Node Text="Section One" Value="1" ContextMenuID="rtvcmSection" /> 
  <Node Text="Section Two" Value="2" ContextMenuID="rtvcmSection" CanDelete="False" CanEdit="False" /> 
  <Node Text="Section Three" Value="3" ContextMenuID="rtvcmSection" /> 
  <Node Text="Section Four" Value="4" ContextMenuID="rtvcmSection" CanDelete="False" /> 
  <Node Text="Section Five" Value="5" ContextMenuID="rtvcmSection" /> 
</Tree> 

TreeeView declaration:
<telerik:RadTreeView ID="rtvSections" runat="server"  
                        OnClientNodeClicked="ClientNodeClicked" 
                        OnClientContextMenuItemClicking="ClientContextMenuItemClicking"
  <ExpandAnimation Duration="200" /> 
  <CollapseAnimation Duration="200" Type="OutQuint" /> 
  <ContextMenus> 
    <telerik:RadTreeViewContextMenu ID="rtvcmSection" runat="server"
      <Items> 
        <telerik:RadMenuItem Text="Insert Section"
          <Items> 
            <telerik:RadMenuItem Text="Before" Value="InsertSectionBefore" /> 
            <telerik:RadMenuItem Text="After" Value="InsertSectionAfter" /> 
          </Items> 
        </telerik:RadMenuItem> 
        <telerik:RadMenuItem Text="Edit Section" Value="EditSection" /> 
        <telerik:RadMenuItem Text="Delete Section" Value="DeleteSection" /> 
        <telerik:RadMenuItem IsSeparator="true" /> 
        <telerik:RadMenuItem Text="New Item" Value="NewItem" /> 
      </Items> 
    </telerik:RadTreeViewContextMenu> 
  </ContextMenus> 
</telerik:RadTreeView> 

Is there anyway I can do this or am I going to have to dynamically create a context menu for every node that requires a non-standard menu?

Cheers,
Nick

4 Answers, 1 is accepted

Sort by
0
Veselin Vasilev
Telerik team
answered on 17 Oct 2008, 02:50 PM
Hello Nick,

You can subscribe to the OnClientContextMenuShowing event of the treeview and cancel it depending on the attribute of the node.

All the best,
Veselin Vasilev
the Telerik team

Check out Telerik Trainer, the state of the art learning tool for Telerik products.
0
Nick
Top achievements
Rank 1
answered on 17 Oct 2008, 02:58 PM
Sorry, I'm not sure I've explained myself well enough  :S

I still want to show a context menu for every node, but for some nodes I may want to disable one of the RadMenuItems in the context menu, dependant on the attributes of the node.

For example if the node has a CanDelete attribute with a value of false, I want the "Delete Section" item in the context menu to be disabled when the menu is shown.

Does that make more sense?

Thanks,
Nick
0
Accepted
Veselin Vasilev
Telerik team
answered on 17 Oct 2008, 03:09 PM
Hi Nick,

I understand now. So, in the OnClientContextMenuShowing you can use the client-side API of the RadContextMenu object to disable the item:

function ClientContextMenuShowing(sender, eventArgs) 
 var node = eventArgs.get_node();    
 var menu = eventArgs.get_menu();     
    
 if (node.get_getAttributes().getAttrubute("canDelete") == "false"
  {        
      menu.findItemByName("Delete").disable(); 
  } 


Sincerely yours,
Veselin Vasilev
the Telerik team

Check out Telerik Trainer, the state of the art learning tool for Telerik products.
0
Nick
Top achievements
Rank 1
answered on 17 Oct 2008, 03:22 PM
Excellent, that's just what I'm looking for!

Thanks Veselin   :D


Tags
TreeView
Asked by
Nick
Top achievements
Rank 1
Answers by
Veselin Vasilev
Telerik team
Nick
Top achievements
Rank 1
Share this question
or