TreeView Context Menu Click event issue

6 posts, 0 answers
  1. Jim
    Jim avatar
    21 posts
    Member since:
    Oct 2007

    Posted 28 Oct 2008 Link to this post

    Hi,

    I'm using a TreeView with a Context Menu that gets generated dynamically.  All of the proper data seems to be loaded for the menu, but when I click on an item, it seems as though the click event isn't being triggered.  In our program we are only trying to handle the client side event, but I tested to see if the server side event was fired, and it also is not executing.  Is this a bug with the TreeView, or am I doing something wrong?
  2. Yana
    Admin
    Yana avatar
    4806 posts

    Posted 29 Oct 2008 Link to this post

    Hello Jim,

    Please check the attached sample project which shows RadTreeView with context menu  and OnClientContextMenuItemClicking event is fired. What is different in your code?

    Regards,
    Yana
    the Telerik team

    Check out Telerik Trainer, the state of the art learning tool for Telerik products.
  3. Jim
    Jim avatar
    21 posts
    Member since:
    Oct 2007

    Posted 29 Oct 2008 Link to this post

    Yana,

    The only major diference between mine and yours is that I have the context menu declared in the aspx page, I'm using RadAjax to update the tree and context menu, and they are being loaded using xml built on the fly.  I was looking around the forums and it looks as though this is a known issue which may have been fixed, is that true?  I'm using 2008_2_723 of the RadControls.
  4. Yana
    Admin
    Yana avatar
    4806 posts

    Posted 30 Oct 2008 Link to this post

    Hello Jim,

    I am still not able to reproduce the described issue even with version 2008_2_723 of RadControls. Could you please send us a working example illustrating the problem? Thanks

    Greetings,
    Yana
    the Telerik team

    Check out Telerik Trainer, the state of the art learning tool for Telerik products.
  5. Jim
    Jim avatar
    21 posts
    Member since:
    Oct 2007

    Posted 30 Oct 2008 Link to this post

    Yana,

    I've created a simple demonstration that creates the issue.  You could either place an alert in the OnTreeContextMenuClick function or a break point.  Open the page and click on the button to bring the tree up.  Once the tree loads, right click on an node and try to click any of the options available. 

    Home.aspx
    <%@ Page Language="C#" AutoEventWireup="true"  CodeFile="Home.aspx.cs" Inherits="Home" %> 
     
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 
     
    <html xmlns="http://www.w3.org/1999/xhtml">  
    <head runat="server">  
        <title>Untitled Page</title> 
    </head> 
    <body> 
        <form id="form1" runat="server">  
        <asp:ScriptManager ID="ScriptManager1" runat="server" /> 
        <telerik:RadScriptBlock ID="RadScriptBlock" runat="server">  
            <script type="text/javascript">  
            function TreeContextMenuHandler(sender,e)     
            {  
                var tree = sender;  
                var node = e.get_node();  
                var selectedNodes = tree.get_selectedNodes();  
                if (selectedNodes != 0)   
                {  
                   tree.unselectAllNodes();  
                }  
                node.select();  
                    
                menuX = e.get_domEvent().clientX;  
                menuY = e.get_domEvent().clientY;    
                  
                clientAjaxRequest("UpdateMenu");         
                  
                e.get_domEvent().cancelBubble = true;  
                if (e.get_domEvent().stopPropagation)  
                {  
                  e.get_domEvent().stopPropagation();  
                }  
                e.get_domEvent().returnValue = false;  
                if (e.get_domEvent().preventDefault)  
                {  
                    e.get_domEvent().preventDefault();  
                }  
                e.set_cancel(true);  
                return false;  
            }  
            function TreeContextMenuHidden(node,e)  
            {   
                var tree = $find("<%= RadTreeView1.ClientID %>");  
                var selectedNodes = tree.get_selectedNodes();  
                if (selectedNodes != 0)            
                    tree.unselectAllNodes();  
            }  
            function OnTreeContextMenuClick(sender, eventArgs)  
            {   
                var treeView = sender;  
                var node = eventArgs.get_node();  
                var item =  eventArgs.get_menuItem();     
                var text =  item.get_text();   
                var value =  item.get_value();    
                if (text == "Disable")  
                {  
                    node.set_enabled(false);  
                    item.get_menu().hide();  
                    return false;  
                }  
                else if (text == "Enable All")  
                {  
                    for (var i = 0; i < treeView.get_nodes().get_count(); i++)  
                    {  
                        treeView.get_nodes().getNode(i).set_enabled(true);  
                    }  
                    item.get_menu().hide();  
                    return false;  
                }  
                else if (text == "Expand")  
                {  
                    node.set_expanded(true);  
                    item.get_menu().hide();  
                    return false;  
                }  
                else if (text == "Collapse")  
                {  
                    node.set_expanded(false);  
                    item.get_menu().hide();  
                    return false;  
                }  
            }         
            function OnAjaxResponseEnd(sender, args)  
            {   
                switch(args.EventArgument)  
                {  
                   case "UpdateMenu":  
                        var menu = null;  
                        menu = $find("<%= RadContextMenu.ClientID %>");   
                        menu.showAt(menuX, menuY);                     
                        break;  
                    default:  
                       break;  
               }                       
            }                 
            function clientAjaxRequest(arg)  
            {  
                var ajaxManagerObject = $find("<%= RadAjaxManager1.ClientID %>");  
                ajaxManagerObject.ajaxRequest(arg);  
                //End SI06439  
            }  
            </script> 
        </telerik:RadScriptBlock> 
        <telerik:RadTreeView ID="RadTreeView1" runat="server" OnClientContextMenuShowing="TreeContextMenuHandler" 
            OnClientContextMenuItemClicked="OnTreeContextMenuClick" > 
            <ContextMenus> 
                <telerik:RadTreeViewContextMenu ID="RadContextMenu" runat="server" OnClientHidden="TreeContextMenuHidden" /> 
            </ContextMenus> 
        </telerik:RadTreeView> 
        <asp:Button ID="Button1" runat="server" Text="Load Tree" OnClick="LoadTree" /> 
          
        <telerik:RadAjaxManager ID="RadAjaxManager1" runat="server" OnAjaxRequest="RadAjaxManager1_AjaxRequest" > 
            <ClientEvents OnResponseEnd="OnAjaxResponseEnd"/>  
            <AjaxSettings> 
                <telerik:AjaxSetting AjaxControlID="RadAjaxManager1">  
                    <UpdatedControls> 
                        <telerik:AjaxUpdatedControl ControlID="RadTreeView1" /> 
                        <telerik:AjaxUpdatedControl ControlID="RadContextMenu" /> 
                    </UpdatedControls> 
                </telerik:AjaxSetting> 
            </AjaxSettings> 
        </telerik:RadAjaxManager> 
        </form> 
    </body> 
    </html> 

    Home.aspx.cs
    using System;  
    using System.Configuration;  
    using System.Data;  
    using System.Web;  
    using System.Web.Security;  
    using System.Web.UI;  
    using System.Web.UI.HtmlControls;  
    using System.Web.UI.WebControls;  
    using System.Web.UI.WebControls.WebParts;  
    using Telerik.Web.UI;  
     
    public partial class Home : System.Web.UI.Page   
    {  
        protected void Page_Load(object sender, EventArgs e)  
        {  
            object o = sender;  
        }  
     
     
        protected void RadAjaxManager1_AjaxRequest(object sender, AjaxRequestEventArgs e)  
        {  
            string ajaxArgument = e.Argument;  
            switch (ajaxArgument)  
            {  
                case "UpdateMenu":  
                    RadContextMenu.Items.Clear();  
                    RadContextMenu.LoadXml("<Menu><Group><Item Text=\"Disable\" /><Item Text=\"Enable All\" /><Item Text=\"Expand\" /><Item Text=\"Collapse\" /></Group></Menu>");  
                    break;  
                defaultbreak;  
            }  
        }  
        protected void LoadTree(object sender, EventArgs e)  
        {  
            RadTreeView1.LoadXml("<Tree><Node Text=\"Parent\" Expanded=\"True\"><Node Text=\"Child1\" /><Node Text=\"Child2\" /><Node Text=\"Child3\" /></Node></Tree>");  
        }  
    }  
     
  6. Yana
    Admin
    Yana avatar
    4806 posts

    Posted 03 Nov 2008 Link to this post

    Hello Jim,

    Thank you for sending your code.

    Unfortunately, there is a limitation when the built-in context menu is showed with Show() method, OnClientContextMenuItemClicked and OnContextMenuItemClick are not fired. So I suggest to use RadContextMenu in your case. I modified your code, so please download the attached file and give it a try.

    Sincerely yours,
    Yana
    the Telerik team

    Check out Telerik Trainer, the state of the art learning tool for Telerik products.
Back to Top