Deleting Nodes in Treeview Control

M

miketayloruk

I'm trying to delete/remove the selected node of a treeview.

What I'm finding is that I cant delete a node unless it is a root
node. For example in the example below, I can delete complete branches
as long as I select "leve 1" nodes.

If I try to delete at Level 2, it doesnt work.

Whay is this? How can I delete nodes that are further down the
branchs?

Thx in advance:

--------------------

<%@ Page Language="VB" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://
www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<script runat="server">

Protected Sub Button1_Click(ByVal sender As Object, ByVal e As
System.EventArgs)
Dim tn As New TreeNode
tn = TreeView1.SelectedNode
TreeView1.Nodes.Remove(tn)

End Sub
</script>

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>Untitled Page</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:TreeView ID="TreeView1" runat="server" ShowLines="True">
<SelectedNodeStyle BackColor="#FFC080" />
<Nodes>
<asp:TreeNode Text="Level 1" Value="1"></asp:TreeNode>
<asp:TreeNode Text="Level 1 B" Value="2">
<asp:TreeNode Text="Level 2 " Value="5">
<asp:TreeNode Text="Level 3" Value="6">
<asp:TreeNode Text="Level 4" Value="7"></
asp:TreeNode>
</asp:TreeNode>
</asp:TreeNode>
</asp:TreeNode>
<asp:TreeNode Text="Level 1 C" Value="3">
<asp:TreeNode Text="Level 2 B" Value="Level 2 B"></
asp:TreeNode>
</asp:TreeNode>
<asp:TreeNode Text="Level 1 D" Value="4"></
asp:TreeNode>
</Nodes>
</asp:TreeView>

</div>
<asp:Button ID="Button1" runat="server"
OnClick="Button1_Click" Text="Delete Selected Node" />
</form>
</body>
</html>
 
M

miketayloruk

I figured it out:

TreeView1.SelectedNode.Parent.ChildNodes.Remove(TreeView1.SelectedNode)

Thx
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,483
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top