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

Treeview: After node Expand Context Menu does not show

1 Answer 58 Views
TreeView
This is a migrated thread and some comments may be shown as answers.
Rohit
Top achievements
Rank 1
Rohit asked on 01 Dec 2010, 02:26 PM
private void LoadRootNodes(RadTreeView treeView)
    {
        const string sql = "Select Name, ItemID from TNodes where ParentID IS NULL ";
        SqlConnection dbCon = new SqlConnection("Data Source=localhost;Initial Catalog=Telerik;user id=sa; password=knowdev");
        SqlDataAdapter adapter = new SqlDataAdapter(sql, dbCon);
        DataTable dataTable = new DataTable();
        adapter.Fill(dataTable);
        RadTreeNode node = new RadTreeNode();
  
  
  
          
        foreach (DataRow row in dataTable.Rows)
        {
            //RadTreeViewContextMenu contextmenu1 = new RadTreeViewContextMenu();
            //contextmenu1.ID = row["Name"].ToString();
  
            //RadTreeView2.ContextMenus.Add(contextmenu1);
  
            //contextmenu1.Items.Add(new RadMenuItem(row["Name"].ToString()));
  
  
            node.Text = row["Name"].ToString();
  
            node.Value = row["ItemID"].ToString();
  
            node.ExpandMode = TreeNodeExpandMode.ServerSideCallBack;
            RadTreeView2.Nodes.Add(node);
            
  
        }
  
        foreach (RadTreeNode nd in RadTreeView2.GetAllNodes())
        {
  
            RadTreeViewContextMenu contextmenu2 = new RadTreeViewContextMenu();
            contextmenu2.ID = nd.Text;
  
            RadTreeView2.ContextMenus.Add(contextmenu2);
            nd.ContextMenuID = contextmenu2.ID;
  
            contextmenu2.Items.Add(new RadMenuItem(nd.Text));
  
  
        }
}
  
  
  
//ON Expand
  
 protected void RadTreeView2_NodeExpand(object sender, RadTreeNodeEventArgs e)
    {
  
        const string sql =
           @"
               SELECT TNodes.ItemID AS NodeId, TNodes.Name AS NodeText, COUNT(Children.ItemID) AS ChildCount
               FROM TNodes LEFT JOIN TNodes As Children
               ON TNodes.ItemID = Children.ParentId
               WHERE TNodes.ParentId = @parentId
               GROUP BY TNodes.ItemID, TNodes.Name
           ";
        SqlConnection dbCon = new SqlConnection("Data Source=localhost;Initial Catalog=Telerik;user id=sa; password=knowdev");
        SqlDataAdapter adapter = new SqlDataAdapter(sql, dbCon);
        adapter.SelectCommand.Parameters.AddWithValue("parentId", e.Node.Value);
        DataTable dataTable = new DataTable();
        adapter.Fill(dataTable);
       
        foreach (DataRow row in dataTable.Rows)
        {
            RadTreeNode node = new RadTreeNode();
            node.Text = row["NodeText"].ToString();
            node.Value = row["NodeId"].ToString();
            
          
            if (Convert.ToInt32(row["ChildCount"]) > 0)
            {
                node.ExpandMode = TreeNodeExpandMode.ServerSideCallBack;
            }
  
  
            if (!(e.Node.TreeView.ContextMenus[0].ID == "test"))
            {
                e.Node.TreeView.ContextMenus[0].Items[0].Text = "test";
                e.Node.TreeView.ContextMenus[0].Items[0].Value = "1";
  
            }
               
  
            e.Node.Nodes.Add(node);
            
              
        }
        e.Node.Expanded = true;
        //e.Node.ExpandMode = TreeNodeExpandMode.ClientSide;
        foreach (RadTreeNode nd in e.Node.GetAllNodes())
        {
            RadTreeViewContextMenu contextmenu = new RadTreeViewContextMenu();
            contextmenu.ID = nd.Text;
  
            RadTreeView2.ContextMenus.Add(contextmenu);
            nd.ContextMenuID = contextmenu.ID;
  
            contextmenu.Items.Add(new RadMenuItem(nd.Text));
  
        }
        
  
    }


Hi,

I am using RadTreeview context menu items. Issue is that after node expand context menu does not show. In above mentioned code on page load

 

 

if(!IsPostBack)

 

LoadRootNodes(RadTreeView2);
LoadRootNodes() functioned is called at that time context menu show properly. but when I expand the node it does not show.
Code is mentioned above. Please suggest.

Thanks,

1 Answer, 1 is accepted

Sort by
0
Dimitar Terziev
Telerik team
answered on 06 Dec 2010, 05:05 PM
Hi Rohit,

When you are using ServerSideCallBack for expand mode, you cannot create context menus in NodeExpand event handler. When this event is fired you could only update the content of the node which fired it.  The reason for this is that the treeview makes an asynchronous request to the server.

In order to add context menus, you should use ServerSide expand mode and RadAjaxManager. This way when making an ajax request you could update the content of the node and add a context menu.
For more information you could check here.

Best wishes,
Dimitar Terziev
the Telerik team
Browse the vast support resources we have to jumpstart your development with RadControls for ASP.NET AJAX. See how to integrate our AJAX controls seamlessly in SharePoint 2007/2010 visiting our common SharePoint portal.
Tags
TreeView
Asked by
Rohit
Top achievements
Rank 1
Answers by
Dimitar Terziev
Telerik team
Share this question
or