Deleting nodes from Treeview control

Last post 09-25-2009 11:47 AM by coolvaas1. 2 replies.

Sort Posts:

  • Deleting nodes from Treeview control

    09-25-2009, 8:35 AM
    • Participant
      931 point Participant
    • coolvaas1
    • Member since 03-26-2004, 7:53 AM
    • Posts 363

    Does any one know how to traverse through a treeviewlist and delete a node when there are no child nodes. I tried many methods, but was not successful in doing it. Any help is greatly appreciated.

     

  • Re: Deleting nodes from Treeview control

    09-25-2009, 11:22 AM
    • Member
      432 point Member
    • giveup
    • Member since 08-25-2009, 5:39 PM
    • Posts 121

    Hi Coolvas,

    Remove A Node from the ASP.NET TreeView Dynamically

    In order to remove a node dynamically from the TreeView bound to a sitemap, use the ontreenodedatabound event as shown below. In this sample, I am removing the “Food Lovers” node for demonstration sake:
             <asp:TreeView ID="TreeView1" runat="server" DataSourceID="SiteMapDataSource1"
                 ontreenodedatabound="TreeView1_TreeNodeDataBound">
             </asp:TreeView>
    C#
        protected void TreeView1_TreeNodeDataBound(object sender, TreeNodeEventArgs e)
        {
            if (e.Node.Text == "Food Lovers")
            {
                e.Node.Parent.ChildNodes.Remove(e.Node);
            }
        }
     
    VB.NET
          Protected Sub TreeView1_TreeNodeDataBound(ByVal sender As Object, ByVal e As TreeNodeEventArgs)
                If e.Node.Text = "Food Lovers" Then
                      e.Node.Parent.ChildNodes.Remove(e.Node)
                End If
          End Sub

     

     

    Thanks!

    Please remember to click “Mark as Answer” on the post that helps you, it will help other(s) to get there answer.
  • Re: Deleting nodes from Treeview control

    09-25-2009, 11:47 AM
    • Participant
      931 point Participant
    • coolvaas1
    • Member since 03-26-2004, 7:53 AM
    • Posts 363

     Hi giveup,

    Thanks for the reply. Much appreciated. Unfortunately I am not using a sitemap. I load the data straight from the database and then want to delete the ones that do not have child nodes.

Page 1 of 1 (3 items)