Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
|
Private Sub ButAdd_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ButAdd.Click’在选定的节点下添加子节点 Dim tmpNd As New TreeNode(), NdSel As TreeNode tmpNd.ID = GetNewId() NdSel = TreeView1.GetNodeFromIndex(TreeView1.SelectedNodeIndex)’选中的节点 tmpNd.Text = "新节点" NdSel.Nodes.Add(tmpNd) Dim myRow As DataRow myRow = ds.Tables("tree").NewRow() myRow("NODE_NAME") = tmpNd.ID myRow("NODE_DESCRIPT") = "新节点" & tmpNd.ID & "_" & NdSel.ID myRow("PARENT_NAME") = NdSel.ID ds.Tables("tree").Rows.Add(myRow) End Sub Private Sub ButDele_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles ButDele.Click’删除选中的节点 Dim idx As String = TreeView1.SelectedNodeIndex() GetNdCol(idx).Remove(TreeView1.GetNodeFromIndex(idx)) Dim dv As New DataView(), recNo As Integer dv.Table = ds.Tables("tree") dv.RowFilter= "NODEID=" & NdId dv.Delete(0) End Sub Private Function GetNdCol(ByVal idx As String) As TreeNodeCollection ‘获得选中节点的父节点的Nodes集合 Dim cnt As Integer, i As Integer Dim tmpNds As TreeNodeCollection Dim idxs() As String idxs = Split(idx, ".") cnt = UBound(idxs) If cnt = 0 Then tmpNds = TreeView1.Nodes Else tmpNds = TreeView1.Nodes(CInt(idxs(0))).Nodes For i = 1 To cnt - 1 tmpNds = tmpNds(CInt(idxs(i))).Nodes Next End If Return tmpNds End Function |
三、修改、移动树节点
由于服务器控件不支持鼠标拖动事件,所以不能象Windows程序那样通过拖动移动节点,这里是通过选择父节点的方式。移动是通过在原位置删除,新位置添加实现的,要注意在删除时先保存节点信息。
Private Sub TreeView1_SelectedIndexChange(ByVal sender As Object, ByVal e As Microsoft.Web.UI.WebControls.TreeViewSelectEventArgs) Handles TreeView1.SelectedIndexChange Dim dv As New DataView() dv.Table = ds.Tables("tree") Dim tmpNd As TreeNode = TreeNdSel(e.OldNode), tmpNds As TreeNodeCollection dv.RowFilter= "NODEID=" & tmpNd.ID dv(0)("NODE_DESCRIPT") = Me.TextBox1.Text dv(0)("ADDRESS") = Me.TextBox2.Text dv(0)("TARGET") = Me.TextBox3.Text dv(0)("ICON") = Me.TextBox4.Text If dv(0)("PARENTID").ToString <> Me.DropDownList1.SelectedItem.Value Then ‘移动节点 dv(0)("PARENT_NAME") = Me.DropDownList1.SelectedItem.Value If Me.DropDownList1.SelectedItem.Value = "ROOT" Then tmpNds = TreeView1.Nodes Else tmpNds = FromIdToNode(Me.DropDownList1.SelectedItem.Value, TreeView1.Nodes).Nodes’新的父节点的Nodes集合 End If GetNdCol(e.OldNode).Remove(tmpNd) tmpNds.Add(tmpNd) End If tmpNd.Text = Me.TextBox1.Text tmpNd.ImageUrl = Me.TextBox4.Text tmpNd = TreeView1.GetNodeFromIndex(TreeView1.SelectedNodeIndex) dv.RowFilter= "NODEID=" & tmpNd.ID Me.TextBox1.Text = dv(0)("NODENAME").ToString Me.TextBox2.Text = dv(0)("ADDRESS").ToString Me.TextBox3.Text = dv(0)("TARGET").ToString Me.TextBox4.Text = dv(0)("ICON").ToString End Sub Private Function FromIdToNode(ByVal ID As String, ByVal Nds As TreeNodeCollection) As TreeNode ‘由关键字查找节点 Dim i As Integer Dim tmpNd As TreeNode, tmpNd1 As TreeNode For Each tmpNd In Nds If tmpNd.ID = ID Then Return tmpNd Exit Function End If tmpNd1 = FromIdToNode(ID, tmpNd.Nodes) If Not (tmpNd1 Is Nothing) Then Return tmpNd1 Exit Function End If Next Return Nothing End Function |