asp.net - Remove all child nodes from a parent node in a TreeView

Remove all child nodes from specific parent node in TreeView
treeview control is made up of nodes. nodes are four types those as root node, parent node, child node and leaf node. parent node always contains other nodes. we can manage parent nodes child nodes using treeview built in methods and properties. the following example code demonstrate us how can we remove all child nodes from specific parent node.

in this process first we need to find the specific parent node using FindNode method. then we call it's child nodes. when we get specific parent node's child nodes, then we apply Clear method to it. ChildNodes.Clear method remove (delete) all child nodes from specific parent node. following example code will help you to better understand the entire process.
TreeViewRemoveChildNodes.aspx

<%@ Page Language="C#" %>
<%@ Import Namespace="System.Drawing" %>

<!DOCTYPE html>

<script runat="server">
    protected void Page_Load(object sender, System.EventArgs e)
    {
        if(!this.IsPostBack)
        {
            TreeView1.ForeColor = Color.White;
            TreeView1.BackColor = Color.SaddleBrown;
            TreeView1.BorderWidth = 2;
            TreeView1.BorderColor = Color.DarkOrchid;
            TreeView1.Font.Italic = true;
            TreeView1.Width = 275;
            TreeView1.ParentNodeStyle.Font.Bold = true;
        }
    }
    protected void Button1_Click(object sender, System.EventArgs e)
    {
        Label1.Text = "";
        string nodePath = TextBox1.Text.ToString();
        try 
        {
            if (TreeView1.FindNode(nodePath).ChildNodes.Count > 0)
            {
                TreeView1.FindNode(nodePath).ChildNodes.Clear();
                Label1.Text = "Parent node found and clear all child nodes.";
            }
            else 
            {
                Label1.Text = "This is not a parent node.";
            }
        }
        catch(Exception ex)
        {
            Label1.Text = "Parent node not found: " + nodePath;
            Label1.Text += "<br />Error: " + ex.ToString();
        }
    }
</script>

<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
    <title>How to clear (remove all) child nodes from specific parent node in TreeView</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <h2 style="color:Navy">TreeView Example: Clear Child Nodes</h2>
        <asp:SiteMapDataSource ID="SiteMapDataSource1" runat="server" />
        <asp:Label 
             ID="Label1" 
             runat="server"
             ForeColor="Red"
             Font-Italic="true"
             Width="300"
             >
        </asp:Label>
        <br /><br />
        <asp:TreeView 
             ID="TreeView1"
             runat="server"
             DataSourceID="SiteMapDataSource1"
             >
        </asp:TreeView>
        <br />
        <asp:Label 
             ID="Label2"
             runat="server"
             Font-Bold="true"
             ForeColor="DodgerBlue"
             Text="Parent Node"
             >
        </asp:Label>
        <asp:TextBox 
             ID="TextBox1"
             runat="server"
             BackColor="LightGoldenrodYellow"
             ForeColor="DodgerBlue"
             Width="200"
             >
        </asp:TextBox>
        <br /><br />
        <asp:Button 
             ID="Button1" 
             runat="server"
             ForeColor="DodgerBlue"
             Text="Find And Clear ChildNodes"
             Height="45"
             OnClick="Button1_Click"
             Font-Bold="true"
             />
    </div>
    </form>
</body>
</html>
Web.sitemap
Web.sitemap source code here.

   


Related asp.net example