With RadTreeView it is easy to add, remove or disable nodes at runtime.
This example shows how to add, remove or disable a node upon NodeClick
.
For this purpose the NodeClick
event is wired up.
[C#]
protected void RadTreeView1_NodeClick(object sender, RadTreeNodeEventArgs NodeEvent)
{
RadTreeNode clickedNode = NodeEvent.Node;
if (!(clickedNode.Owner is RadTreeView))
{
switch ( ((RadTreeNode) clickedNode.Owner).Value)
{
case "Add":
RadTreeNode newNode = new RadTreeNode();
newNode.Text = clickedNode.Text + " (Clone)";
//Adds the node to the Nodes collection of the clicked node's owner - it might be the treeview itself
clickedNode.Owner.Nodes.Add(newNode);
break;
case "Delete":
RadTreeNode parent = (RadTreeNode)clickedNode.Parent;
//Removes the node from the Nodes collection of the clicked node's parent
parent.Nodes.Remove(clickedNode);
break;
case "Disable":
//Disables the node
clickedNode.Enabled = false;
break;
}
}
}
[VB]
Protected Sub RadTreeView1_NodeClick(ByVal sender As Object, ByVal NodeEvent As RadTreeNodeEventArgs) Handles RadTreeView1.NodeClick
Dim clickedNode As RadTreeNode = NodeEvent.Node
If Not TypeOf clickedNode.Owner Is RadTreeView Then
Select Case CType(clickedNode.Owner, RadTreeNode).Value
Case "Add"
Dim newNode As New RadTreeNode()
newNode.Text = clickedNode.Text + " (Clone)"
'Adds the node to the Nodes collection of the clicked node's owner - it might be the treeview itself
clickedNode.Owner.Nodes.Add(newNode)
Case "Delete"
'Removes the node from the Nodes collection of the clicked node's parent
CType(clickedNode.Parent, RadTreeNode).Nodes.Remove(clickedNode)
Case "Disable"
'Disables the node
clickedNode.Enabled = False
End Select
End If
End Sub
Select a node and click a button.