Telerik is a leading vendor of ASP.NET AJAX, ASP.NET MVC, Silverlight, WinForms and WPF controls and components, as well as .NET Reporting, .NET ORM , .NET CMS, Code Analysis, Mocking, Team Productivity and Automated Testing Tools. Building on its expertise in interface development and Microsoft technologies, Telerik helps customers build applications with unparalleled richness, responsiveness and interactivity. Telerik products help thousands of companies to be more productive and deliver reliable applications under budget and on time.
Version Q2 2011 released 07/12/2011
select

TreeView / Context Menu

  • Personal Folders
    • Deleted Items (79)
    • Drafts (2)
    • Inbox (23)
    • Junk Email (34)
    • Outbox
    • Public folders (2)
    • RSS Feeds
    • Sent Items

  • Right-click a node with the mouse to display the context menu.

Source Code

C# VB.NET
Show code in new window Demo isolation steps
  • <%@ Page AutoEventWireup="true" CodeFile="DefaultCS.aspx.cs" Inherits="Telerik.Web.Examples.TreeView.ContextMenu.DefaultCS"
        Language="c#" %>

    <%@ Register TagPrefix="telerik" Namespace="Telerik.Web.UI" Assembly="Telerik.Web.UI" %>
    <%@ Register TagPrefix="qsf" TagName="Footer" Src="~/Common/Footer.ascx" %>
    <%@ Register TagPrefix="qsf" TagName="HeadTag" Src="~/Common/HeadTag.ascx" %>
    <%@ Register TagPrefix="qsf" TagName="Header" Src="~/Common/Header.ascx" %>
    <%@ Register TagPrefix="qsf" Namespace="Telerik.QuickStart" %>
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head runat="server">
        <qsf:HeadTag runat="server" ID="Headtag1"></qsf:HeadTag>
    </head>
    <body class="BODY">
        <form runat="server" id="mainForm" method="post">
            <telerik:RadScriptManager ID="RadScriptManager1" runat="server">
            </telerik:RadScriptManager>
            <qsf:Header runat="server" ID="Header1" NavigationLanguage="C#"></qsf:Header>

            <script type="text/javascript">
                //<!--
                    function onClientContextMenuShowing(sender, args)
                    {
                        var treeNode = args.get_node();
                        treeNode.set_selected(true);
                        //enable/disable menu items
                        setMenuItemsState(args.get_menu().get_items(), treeNode);
                    }
                    
                    function onClientContextMenuItemClicking(sender, args)
                    {
                        var menuItem = args.get_menuItem();
                        var treeNode = args.get_node();
                        menuItem.get_menu().hide();
                        
                        switch(menuItem.get_value())
                        {
                            case "Copy":
                                break;
                            case "Rename":
                                treeNode.startEdit();
                                break;
                            case "NewFolder":
                                break;
                            case "MarkAsRead":
                                break;
                            case "Delete":
                                var result = confirm("Are you sure you want to delete the item: " + treeNode.get_text());
                                args.set_cancel(!result);
                                break;                            
                        }
                    }
                    
                    //this method disables the appropriate context menu items
                    function setMenuItemsState(menuItems, treeNode)
                    {
                        for (var i=0; i<menuItems.get_count(); i++)
                        {
                            var menuItem = menuItems.getItem(i);
                            switch(menuItem.get_value())
                            {
                                case "Copy":
                                    formatMenuItem(menuItem, treeNode, 'Copy "{0}"');
                                    break;
                                case "Rename":
                                    formatMenuItem(menuItem, treeNode, 'Rename "{0}"');
                                    break;
                                case "Delete":
                                    formatMenuItem(menuItem, treeNode, 'Delete "{0}"');
                                    break;                                
                                case "NewFolder":
                                    if (treeNode.get_parent() == treeNode.get_treeView())
                                    {
                                        menuItem.set_enabled(false);
                                    }
                                    else
                                    {
                                        menuItem.set_enabled(true);
                                    }
                                    break;
                                case "MarkAsRead":
                                    var enabled = hasNodeMails(treeNode);
                                    menuItem.set_enabled(enabled != null);
                                    break;
                            }
                        }
                    }
                    
                    //formats the Text of the menu item
                    function formatMenuItem(menuItem, treeNode, formatString)
                    {
                        var nodeValue = treeNode.get_value();
                        if (nodeValue && nodeValue.indexOf("_Private_") == 0)
                        {
                            menuItem.set_enabled(false);
                        }
                        else
                        {
                            menuItem.set_enabled(true);
                        }
                        var newText = String.format(formatString, extractTitleWithoutMails(treeNode));
                        menuItem.set_text(newText);
                    }
                
                 //checks if the text contains (digit)
                    function hasNodeMails(treeNode)
                    {
                        return treeNode.get_text().match(/\([\d]+\)/ig);
                    }
                    
                    //removes the brackets with the numbers,e.g. Inbox (30)
                    function extractTitleWithoutMails(treeNode)
                    {
                        return treeNode.get_text().replace(/\s*\([\d]+\)\s*/ig, "");
                    }                                                
                //-->
            </script>

            <telerik:RadAjaxLoadingPanel ID="RadAjaxLoadingPanel1" runat="server">
            </telerik:RadAjaxLoadingPanel>
            <telerik:RadAjaxPanel ID="RadAjaxPanel1" runat="server" LoadingPanelID="RadAjaxLoadingPanel1">
                <telerik:RadTreeView ID="RadTreeView1" runat="server" AllowNodeEditing="true"
                    OnContextMenuItemClick="RadTreeView1_ContextMenuItemClick" OnClientContextMenuItemClicking="onClientContextMenuItemClicking"
                    OnClientContextMenuShowing="onClientContextMenuShowing" OnNodeEdit="RadTreeView1_NodeEdit">
                    <ContextMenus>
                        <telerik:RadTreeViewContextMenu ID="MainContextMenu" runat="server">
                            <Items>
                                <telerik:RadMenuItem Value="Copy" Text="Copy ..." ImageUrl="~/TreeView/Img/Outlook/10.gif">
                                </telerik:RadMenuItem>
                                <telerik:RadMenuItem Value="Rename" Text="Rename ..." Enabled="false" ImageUrl="~/TreeView/Img/Outlook/rename.gif"
                                    PostBack="false">
                                </telerik:RadMenuItem>
                                <telerik:RadMenuItem Value="NewFolder" Text="New Folder" ImageUrl="~/TreeView/Img/Outlook/12.gif">
                                </telerik:RadMenuItem>
                                <telerik:RadMenuItem Value="Delete" Text="Delete Folder" ImageUrl="~/TreeView/Img/Outlook/7.gif">
                                </telerik:RadMenuItem>
                                <telerik:RadMenuItem IsSeparator="true">
                                </telerik:RadMenuItem>
                                <telerik:RadMenuItem Value="MarkAsRead" Text="Mark All as Read">
                                </telerik:RadMenuItem>
                            </Items>
                            <CollapseAnimation Type="none" />
                        </telerik:RadTreeViewContextMenu>
                        <telerik:RadTreeViewContextMenu ID="EmptyFolderContextMenu" runat="server">
                            <Items>
                                <telerik:RadMenuItem Value="NewFolder" Text="New Folder" ImageUrl="~/TreeView/Img/Outlook/12.gif">
                                </telerik:RadMenuItem>
                                <telerik:RadMenuItem IsSeparator="true">
                                </telerik:RadMenuItem>
                                <telerik:RadMenuItem Value="EmptyFolder" Text="Empty this folder" ImageUrl="~/TreeView/Img/Outlook/2DeletedItems.gif">
                                </telerik:RadMenuItem>
                            </Items>
                            <CollapseAnimation Type="none" />
                        </telerik:RadTreeViewContextMenu>
                    </ContextMenus>
                    <Nodes>
                        <telerik:RadTreeNode Value="_Private_PersonalFolders" ImageUrl="~/TreeView/Img/Outlook/1PersonalFolders.gif"
                            AllowEdit="false" Text="Personal Folders" Expanded="true">
                            <Nodes>
                                <telerik:RadTreeNode Value="_Private_DeletedItems" ImageUrl="~/TreeView/Img/Outlook/2DeletedItems.gif"
                                    AllowEdit="false" Text="Deleted Items (79)" Font-Bold="true" ContextMenuID="EmptyFolderContextMenu">
                                    <Nodes>
                                        <telerik:RadTreeNode Text="Spam (33)" AllowEdit="true" Font-Bold="true" ImageUrl="~/TreeView/Img/Outlook/folder.gif">
                                        </telerik:RadTreeNode>
                                        <telerik:RadTreeNode Text="Old (46)" AllowEdit="true" Font-Bold="true" ImageUrl="~/TreeView/Img/Outlook/folder.gif">
                                        </telerik:RadTreeNode>
                                    </Nodes>
                                </telerik:RadTreeNode>
                                <telerik:RadTreeNode Value="_Private_Drafts" ImageUrl="~/TreeView/Img/Outlook/3Drafts.gif"
                                    AllowEdit="false" Text="Drafts (2)" Font-Bold="true">
                                </telerik:RadTreeNode>
                                <telerik:RadTreeNode Value="_Private_Inbox" ImageUrl="~/TreeView/Img/Outlook/4Inbox.gif"
                                    AllowEdit="false" Text="Inbox (23)" Font-Bold="true">
                                    <Nodes>
                                        <telerik:RadTreeNode Text="Family (1)" Font-Bold="true" ImageUrl="~/TreeView/Img/Outlook/folder.gif">
                                        </telerik:RadTreeNode>
                                        <telerik:RadTreeNode Text="Friends (2)" Font-Bold="true" ImageUrl="~/TreeView/Img/Outlook/folder.gif">
                                        </telerik:RadTreeNode>
                                        <telerik:RadTreeNode Text="Partners" ImageUrl="~/TreeView/Img/Outlook/folder.gif">
                                        </telerik:RadTreeNode>
                                        <telerik:RadTreeNode Text="New Offers (8)" Font-Bold="true" ImageUrl="~/TreeView/Img/Outlook/folder.gif">
                                        </telerik:RadTreeNode>
                                        <telerik:RadTreeNode Text="Job candidates (12)" Font-Bold="true" ImageUrl="~/TreeView/Img/Outlook/folder.gif">
                                        </telerik:RadTreeNode>
                                    </Nodes>
                                </telerik:RadTreeNode>
                                <telerik:RadTreeNode Value="_Private_JunkEmail" AllowEdit="false" Text="Junk Email (34)"
                                    Font-Bold="true" ImageUrl="~/TreeView/Img/Outlook/junk.gif" ContextMenuID="EmptyFolderContextMenu">
                                </telerik:RadTreeNode>
                                <telerik:RadTreeNode Value="_Private_OutBox" ImageUrl="~/TreeView/Img/Outlook/outbox.gif"
                                    AllowEdit="false" Text="Outbox">
                                </telerik:RadTreeNode>
                                <telerik:RadTreeNode Value="_Private_PublicFolders" ImageUrl="~/TreeView/Img/Outlook/folder.gif"
                                    AllowEdit="false" Text="Public folders (2)" Font-Bold="true">
                                    <Nodes>
                                        <telerik:RadTreeNode Text="Favorites" ImageUrl="~/TreeView/Img/Outlook/folder.gif">
                                        </telerik:RadTreeNode>
                                        <telerik:RadTreeNode Text="All Public Folders (2)" ImageUrl="~/TreeView/Img/Outlook/folder.gif"
                                            Font-Bold="true">
                                        </telerik:RadTreeNode>
                                    </Nodes>
                                </telerik:RadTreeNode>
                                <telerik:RadTreeNode Value="_Private_RssFeeds" AllowEdit="false" Text="RSS Feeds"
                                    ImageUrl="~/TreeView/Img/Outlook/rss.gif">
                                </telerik:RadTreeNode>
                                <telerik:RadTreeNode Value="_Private_SentItems" ImageUrl="~/TreeView/Img/Outlook/sent.gif"
                                    AllowEdit="false" Text="Sent Items">
                                </telerik:RadTreeNode>
                            </Nodes>
                        </telerik:RadTreeNode>
                    </Nodes>
                </telerik:RadTreeView>
            </telerik:RadAjaxPanel>
            <qsf:Footer runat="server" ID="Footer1"></qsf:Footer>
        </form>
    </body>
    </html>

Get more than expected!

 
 

Take your time to truly experience the power of RadControls for ASP.NET AJAX with a free 60-day trial backed up by Telerik’s unlimited dedicated support.

Download your RadControls for ASP.NET AJAX trial and jumpstart your development with the available Getting Started resources.

If you have any questions, do not hesitate to contact us at sales@telerik.com.

Copyright 2002-2024 © Telerik. All right reserved
Telerik Inc, 201 Jones Rd, Waltham, MA 02451