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

Multiple Context Menus

7 Answers 123 Views
TreeView
This is a migrated thread and some comments may be shown as answers.
William
Top achievements
Rank 1
William asked on 14 Nov 2011, 09:59 PM
I have a treeview, that depending on if a check box is check will display either only nodes that are flagged as "Enabled" in the database, or all nodes. Depending on if a node is set as enabled I would like to attach one of two context menu's to it. 

This is the error message I'm getting: 
"Multiple controls with the same ID 'contextMenu1' were found. FindControl requires that controls have unique IDs."

This error occurs when I toggle the check box a couple times.

Below is my code:

<span>Show All Locations:</span>
       <asp:CheckBox ID="LocationsTreeView_ChkBox" runat="server" AutoPostBack="true" OnCheckedChanged="LocationsTreeView_CheckedChanged" Checked="false" />
           <telerik:RadTreeView ID="LocationsTreeView" runat="server" EnableDragAndDrop="true"  MultipleSelect="true" EnableDragAndDropBetweenNodes="true"
           AllowNodeEditing="true" OnContextMenuItemClick="LocationsTreeView_ContextMenuItemClick" OnClientContextMenuItemClicking="onClientContextMenuItemClicking"
           OnClientContextMenuShowing="onClientContextMenuShowing" OnNodeEdit="LocationsTreeView_NodeEdit"
           OnNodeDrop="LocationsTreeView_NodeDrop" OnClientNodeDropping="onNodeDropping" OnClientNodeDragging="onNodeDragging" >
    
           </telerik:RadTreeView>


protected void LocationsTreeView_CheckedChanged(object sender, EventArgs e)
        {
            CheckBox cb = (CheckBox)sender;
 
            if (cb.Checked == true)
                LocationTreeAll();
 
            else if (cb.Checked == false)
                LocationTree();
        }


public void LocationTreeContextMenu()
       {
           // Define Context Menu 1 ## Enable Menu Item
           // Define Context Menu 2 ## Disable Menu Item
 
           RadTreeViewContextMenu contextMenu1 = new RadTreeViewContextMenu();
           RadTreeViewContextMenu contextMenu2 = new RadTreeViewContextMenu();
           contextMenu1.ID = "contextMenu1";
           contextMenu2.ID = "contextMenu2";
            
           // Build Menu's
            
           RadMenuItem menuItem1 = new RadMenuItem();
               menuItem1.Value = "Rename";
               menuItem1.Text = "Rename ...";
               menuItem1.Enabled = true;
               menuItem1.ImageUrl="images/icons/edit_48.png";
               contextMenu1.Items.Add(menuItem1);
               contextMenu2.Items.Add(menuItem1);
           RadMenuItem menuItem2 = new RadMenuItem();
               menuItem2.IsSeparator = true;
               contextMenu1.Items.Add(menuItem2);
               contextMenu2.Items.Add(menuItem2);
           RadMenuItem menuItem3 = new RadMenuItem();
               menuItem3.Value = "addLocation";
               menuItem3.Text = "Add Location";
               menuItem3.Enabled = true;
               menuItem3.ImageUrl = "images/icons/add_16.png";
               contextMenu1.Items.Add(menuItem3);
               contextMenu2.Items.Add(menuItem3);
           RadMenuItem menuItem4 = new RadMenuItem();
               menuItem4.Value = "enableLocation";
               menuItem4.Text = "Enable Location";
               menuItem4.Enabled = true;
               contextMenu1.Items.Add(menuItem4);
           RadMenuItem menuItem5 = new RadMenuItem();
               menuItem5.Value = "disableLocation";
               menuItem5.Text = "Disable Location";
               menuItem5.Enabled = true;
               contextMenu2.Items.Add(menuItem5);
           RadMenuItem menuItem6 = new RadMenuItem();
               menuItem6.Value = "editDetails";
               menuItem6.Text = "Edit Details";
               menuItem6.Enabled = true;
               menuItem6.PostBack = true;
               contextMenu1.Items.Add(menuItem6);
               contextMenu2.Items.Add(menuItem6);
 
           // Add The context menu to the tree
           LocationsTreeView.ContextMenus.Add(contextMenu1);
           LocationsTreeView.ContextMenus.Add(contextMenu2);
 
           // Check to see which node gets which context menu
           foreach (RadTreeNode node in LocationsTreeView.GetAllNodes())
           {
               string check = "";
               string getStatus = "SELECT Enabled FROM dbo.Locations WHERE ID='" + node.Value.ToString() + "'";
 
               SqlCommand cmd = new SqlCommand(getStatus,connection);
               connection.Open();
               check = cmd.ExecuteScalar().ToString();
               connection.Close();
               cmd.Dispose();
 
               if (check == "True")
               {
                   node.ContextMenuID = "contextMenu2";
               }
               else if (check == "False")
               {
                   node.ContextMenuID = "contextMenu1";
               }
           }
       }

7 Answers, 1 is accepted

Sort by
0
Plamen
Telerik team
answered on 15 Nov 2011, 03:46 PM
Hello William ,

In cases when different context menus are needed we recommend changing the view of the context menu as in the Demo. Have you considered this way and if yes would you please explain why you don't use it instead?

Regards,
Plamen Zdravkov
the Telerik team
If you want to get updates on new releases, tips and tricks and sneak peeks at our product labs directly from the developers working on the RadControls for ASP.NET AJAX, subscribe to their blog feed now
0
William
Top achievements
Rank 1
answered on 15 Nov 2011, 09:23 PM
I see that I can set items in the context menu as visible via the javascript, is that what you were getting at?

That works very well, but I need to query the database in order to determine if the menu item should be visible or not, so I need to do this in the code behind. 
0
Plamen
Telerik team
answered on 18 Nov 2011, 05:09 PM
Hi William, 

I inspected the code again but it seems that the error is coming from the custom code when checking the Checkbox. Would you share all the server and client events that take part of this scenario so I could reproduce the error it locally?

All the best,
Plamen Zdravkov
the Telerik team
If you want to get updates on new releases, tips and tricks and sneak peeks at our product labs directly from the developers working on the RadControls for ASP.NET AJAX, subscribe to their blog feed now
0
William
Top achievements
Rank 1
answered on 18 Nov 2011, 05:18 PM
// Show all possible locations
public void LocationTreeAll()
{
    // Clear any exisiting nodes
    LocationsTreeView.Nodes.Clear();
 
    using (SqlConnection connection = new SqlConnection())
    {
        // Data Connection
        connection.ConnectionString = (ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString);
        connection.Open();
        string getLocations = @"
            With hierarchy (id, [location id], name, depth, [path])
            As (
 
                Select ID, [LocationID], Name, 1 As depth,
                    Cast(Null as varChar(max)) As [path]
                From dbo.Locations
                Where ID = [LocationID]
 
                Union All
 
                Select child.id, child.[LocationID], child.name,
                    parent.depth + 1 As depth,
                    IsNull(
                        Cast(parent.id As varChar(max)),
                        Cast(parent.id As varChar(max))
                    ) As [path]
                From dbo.Locations As child
                Inner Join hierarchy As parent
                    On child.[LocationID] = parent.ID
                Where child.ID != parent.[Location ID])
 
            Select *
            From hierarchy
            Order By [depth], name asc";
 
        using (SqlCommand cmd = new SqlCommand(getLocations, connection))
        {
            cmd.CommandType = CommandType.Text;
            using (SqlDataReader rs = cmd.ExecuteReader())
            {
                while (rs.Read())
                {
                    string id = rs.GetGuid(0).ToString();
                    string locationID = rs.GetGuid(1).ToString();
                    RadTreeNode node = new RadTreeNode();
                    node.Text = rs.GetString(2);
                    node.Value = id.ToString();
 
 
                    // Get Parent Node
                    if (id == locationID)
                    {
                        LocationsTreeView.Nodes.Add(node);
 
                    }
                    // Find Child Nodes
                    else
                    {
                        (LocationsTreeView.FindNodeByValue(rs.GetString(4).ToString().ToLower())).Nodes.Add(node);
                    }
                }
            }
        }
    }
    LocationTreeContextMenu();
    LocationsTreeView.Nodes[0].Expanded = true;
}

// Show only enabled locations
public void LocationTree()
{
    // Clear any exisiting nodes
    LocationsTreeView.Nodes.Clear();
 
    using (SqlConnection connection = new SqlConnection())
    {
        // Data Connection
        connection.ConnectionString = (ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString);
        connection.Open();
        string getLocations = @"
            With hierarchy (id, [location id], name, depth, [path])
            As (
 
                Select ID, [LocationID], Name, 1 As depth,
                    Cast(Null as varChar(max)) As [path]
                From dbo.Locations
                Where ID = [LocationID] AND Enabled = 'True'
 
                Union All
 
                Select child.id, child.[LocationID], child.name,
                    parent.depth + 1 As depth,
                    IsNull(
                        Cast(parent.id As varChar(max)),
                        Cast(parent.id As varChar(max))
                    ) As [path]
                From dbo.Locations As child
                Inner Join hierarchy As parent
                    On child.[LocationID] = parent.ID
                Where child.ID != parent.[Location ID] AND Enabled = 'True')
 
            Select *
            From hierarchy
            Order By [depth], name asc";
 
        using (SqlCommand cmd = new SqlCommand(getLocations, connection))
        {
            cmd.CommandType = CommandType.Text;
            using (SqlDataReader rs = cmd.ExecuteReader())
            {
                while (rs.Read())
                {
                    string id = rs.GetGuid(0).ToString();
                    string locationID = rs.GetGuid(1).ToString();
                    RadTreeNode node = new RadTreeNode();
                    node.Text = rs.GetString(2);
                    node.Value = id.ToString();
 
 
                    // Get Parent Node
                    if (id == locationID)
                    {
                        LocationsTreeView.Nodes.Add(node);
 
                    }
                    // Find Child Nodes
                    else
                    {
                        (LocationsTreeView.FindNodeByValue(rs.GetString(4).ToString().ToLower())).Nodes.Add(node);
                    }
                }
            }
        }  
    }
    LocationTreeContextMenu();
    LocationsTreeView.Nodes[0].Expanded = true;
}
0
William
Top achievements
Rank 1
answered on 22 Nov 2011, 09:25 PM
If I set ViewStateMode="Disabled", then the context menu's will display properly, but none of the menu items will work. 
0
Accepted
Plamen
Telerik team
answered on 23 Nov 2011, 02:42 PM
Hi William ,

It seems to me that the Contextmenus have to be Cleared at some point because they have been added several times. Here with the yellow is the code that I added that worked at my side:
         LocationsTreeView.ContextMenus.Clear();
      LocationsTreeView.ContextMenus.Add(contextMenu1);
      LocationsTreeView.ContextMenus.Add(contextMenu2);

Please, let me know if this has helped.

Kind regards,
Plamen Zdravkov
the Telerik team
If you want to get updates on new releases, tips and tricks and sneak peeks at our product labs directly from the developers working on the RadControls for ASP.NET AJAX, subscribe to their blog feed now
0
William
Top achievements
Rank 1
answered on 23 Nov 2011, 04:05 PM
Yes, that seems to have fixed the issue. Thank you for your help!
Tags
TreeView
Asked by
William
Top achievements
Rank 1
Answers by
Plamen
Telerik team
William
Top achievements
Rank 1
Share this question
or