New to Telerik UI for ASP.NET AJAX? Download free 30-day trial

Add/Remove/Disable Nodes

  • Books
    • Arts
    • Children's Books
    • Cooking
  • Music
    • Alternative
    • Blues
    • Dance
  • Demo Configurator

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.

  • DefaultVB.aspx
  • DefaultVB.aspx.vb
  • tree.xml
<%@ Page AutoEventWireup="true" CodeFile="DefaultVB.aspx.vb" Inherits="TreeView.Examples.Programming.AddRemoveDisable.DefaultVB"Language="vb"  %>

<!DOCTYPE html>
<html xmlns='http://www.w3.org/1999/xhtml'>
<head runat="server">
    <title>Telerik ASP.NET Example</title>
</head>

<body>
    <form id="form1" runat="server">
    <telerik:RadScriptManager runat="server" ID="RadScriptManager1" />
    <telerik:RadSkinManager ID="RadSkinManager1" runat="server" ShowChooser="true" />
    <div class="demo-container size-thin">
        <telerik:RadTreeView RenderMode="Lightweight" ID="RadTreeView1" runat="server"  Width="300px">
        </telerik:RadTreeView>
        <asp:RequiredFieldValidator ID="RequiredFieldValidator3" runat="server" ErrorMessage="Select a node first."
            Display="Dynamic" ControlToValidate="RadTreeView1" ValidationGroup="DeleteRequiresSelection" ForeColor="Red"
            CssClass="validator">
        </asp:RequiredFieldValidator>
        <asp:RequiredFieldValidator ID="RequiredFieldValidator2" runat="server" ErrorMessage="Select a node first."
            ControlToValidate="RadTreeView1" Display="Dynamic" ValidationGroup="DisableRequiresSelection" ForeColor="Red"
            CssClass="validator">
        </asp:RequiredFieldValidator>
    </div>
    <telerik:RadAjaxManager runat="server" ID="RadAjaxManager1">
        <AjaxSettings>
            <telerik:AjaxSetting AjaxControlID="ConfigurationPanel1">
                <UpdatedControls>
                    <telerik:AjaxUpdatedControl ControlID="RadTreeView1" LoadingPanelID="RadAjaxLoadingPanel1" />
                </UpdatedControls>
            </telerik:AjaxSetting>
        </AjaxSettings>
    </telerik:RadAjaxManager>
    <telerik:RadAjaxLoadingPanel runat="server" ID="RadAjaxLoadingPanel1">
    </telerik:RadAjaxLoadingPanel>

    <qsf:ConfiguratorPanel runat="server" ID="ConfigurationPanel1" Title="Demo Configurator">
        <Views>
            <qsf:View>
                <ul class="fb-group">
                    <li>
                        <qsf:Button ID="DisableButton" runat="server" OnClick="DisableButton_Click"
                            ValidationGroup="DisableRequiresSelection" Text="Disable selected node">
                        </qsf:Button>
                    </li>
                    <li>
                        <qsf:Button ID="EnableAllButton" runat="server" CausesValidation="False"
                            OnClick="EnableAll_Button" Text="Enable all nodes">
                        </qsf:Button>
                    </li>
                    <li>
                        <qsf:Button ID="DeleteButton" runat="server" OnClick="DeleteButton_Click"
                            ValidationGroup="DeleteRequiresSelection" Text="Delete selected node">
                        </qsf:Button>
                    </li>
                    <li>
                        <qsf:TextBox runat="Server" ID="NodeTextBox" EmptyMessage="Enter node text" Size="Wide" />
                        <qsf:Button ID="AddButton" runat="server" OnClick="AddButton_Click"
                            ValidationGroup="TextRequired" Text="Add node" />
                        <asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server" ErrorMessage="Text is required"
                            Display="Dynamic" ControlToValidate="NodeTextBox" ValidationGroup="TextRequired"
                            CssClass="validator">
                        </asp:RequiredFieldValidator>
                    </li>
                    <li>
                    </li>
                </ul>
            </qsf:View>
        </Views>
    </qsf:ConfiguratorPanel>
    </form>
</body>
</html>

Support & Learning Resources

Find Assistance