Dynamically modify RadTreeNode's ContextMenu?

5 posts, 1 answers
  1. Nick
    Nick avatar
    86 posts
    Member since:
    Mar 2008

    Posted 17 Oct 2008 Link to this post

    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:
    1 <Tree> 
    2   <Node Text="Front Page" Value="" ContextMenuID="rtvcmFrontPage"/> 
    3   <Node Text="Section One" Value="1" ContextMenuID="rtvcmSection" /> 
    4   <Node Text="Section Two" Value="2" ContextMenuID="rtvcmSection" CanDelete="False" CanEdit="False" /> 
    5   <Node Text="Section Three" Value="3" ContextMenuID="rtvcmSection" /> 
    6   <Node Text="Section Four" Value="4" ContextMenuID="rtvcmSection" CanDelete="False" /> 
    7   <Node Text="Section Five" Value="5" ContextMenuID="rtvcmSection" /> 
    8 </Tree> 

    TreeeView declaration:
    1 <telerik:RadTreeView ID="rtvSections" runat="server"  
    2                         OnClientNodeClicked="ClientNodeClicked" 
    3                         OnClientContextMenuItemClicking="ClientContextMenuItemClicking"
    4   <ExpandAnimation Duration="200" /> 
    5   <CollapseAnimation Duration="200" Type="OutQuint" /> 
    6   <ContextMenus> 
    7     <telerik:RadTreeViewContextMenu ID="rtvcmSection" runat="server"
    8       <Items> 
    9         <telerik:RadMenuItem Text="Insert Section"
    10           <Items> 
    11             <telerik:RadMenuItem Text="Before" Value="InsertSectionBefore" /> 
    12             <telerik:RadMenuItem Text="After" Value="InsertSectionAfter" /> 
    13           </Items> 
    14         </telerik:RadMenuItem> 
    15         <telerik:RadMenuItem Text="Edit Section" Value="EditSection" /> 
    16         <telerik:RadMenuItem Text="Delete Section" Value="DeleteSection" /> 
    17         <telerik:RadMenuItem IsSeparator="true" /> 
    18         <telerik:RadMenuItem Text="New Item" Value="NewItem" /> 
    19       </Items> 
    20     </telerik:RadTreeViewContextMenu> 
    21   </ContextMenus> 
    22 </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
  2. Veselin Vasilev
    Admin
    Veselin Vasilev avatar
    2992 posts

    Posted 17 Oct 2008 Link to this post

    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.
  3. UI for ASP.NET Ajax is Ready for VS 2017
  4. Nick
    Nick avatar
    86 posts
    Member since:
    Mar 2008

    Posted 17 Oct 2008 Link to this post

    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
  5. Answer
    Veselin Vasilev
    Admin
    Veselin Vasilev avatar
    2992 posts

    Posted 17 Oct 2008 Link to this post

    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.
  6. Nick
    Nick avatar
    86 posts
    Member since:
    Mar 2008

    Posted 17 Oct 2008 Link to this post

    Excellent, that's just what I'm looking for!

    Thanks Veselin   :D


Back to Top
UI for ASP.NET Ajax is Ready for VS 2017