ASP. NET Tree Structure

Source: Internet
Author: User

ASP. NET tree charts are used to display data organized by tree structures. They are widely used, such as file systems in computers (resource managers in Windows) and the composition structure of enterprises or companies. We know that in Windows, tools such as VB, PB, and Delphi provide a powerful tree-type control TreeView. Using the Treeview control, we can easily develop a tree chart. However, it is not so easy to implement tree embedding on the web page. NET uses the Internet Explorer WebControls provided by Microsoft to make the tree structure development on the web page as convenient as it is in Windows. The same functions are powerful and even more flexible.


This article describes how to use Internet Explorer WebControls to develop an ASP. NET tree structure. Due to the complex structure of the tree structure, I often do not know how to use it. I have recently used ASP for my company. the specific example of the Application Manager compiled by. NET is described in detail in ASP. NET, how to associate the use of Internet Explorer WebControls with the database to display data in multiple layers, so as to conveniently add, modify, delete, and move data. I hope that through the elaboration of this instance, I will bring you a better result, communicate with colleagues, and make progress together.

I. Tree Creation

The specific method is to create a database and design the TREE_INFO table of the tree chart, which contains the NODEID, PARENTID, NODENAME, ADDERSS, and ICON fields. Other fields are determined based on the actual business, the node name NODENAME is displayed on the node of the tree control. The NODEID field stores the unique ID of the node. PARENTID indicates the parent node number of the current node. the ID number forms a "linked list ", the structure of the nodes on the tree is recorded. Design a Web form and place the TreeView control on it.

 
 
  1. Private Sub CreateDataSet () 'create a dataset
  2. Dim myConn As New SqlConnection ()
  3. Dim myCmd As New SqlCommand ("select NODEID, NODENAME, PARENTID,
    ADDRESS, ICON from Tree_info ", myConn)
  4. Dim myDataAdapter As New SqlDataAdapter ()
  5. MyConn. ConnectionString=Application("Connectstring ")
  6. MyCmd. CommandText="" 
  7. MyCmd. Connection=MyConn 
  8. MyDataAdapter. SelectCommand=MyCmd 
  9. MyDataAdapter. Fill (ds, "tree ")
  10. End Sub

The basic idea of building a tree is to recursively call the display subtree from the root node.

 
 
  1. Private Sub Page_Load(ByVal sender As System.Object, 
    ByVal e As System.EventArgs) Handles MyBase.Load  
  2. CreateDataSet()  
  3. intiTree(TreeView1.Nodes, 0)  
  4. End Sub  
  5. Private Sub intiTree(ByRef Nds As TreeNodeCollection, 
    ByVal parentId As Integer)  
  6. Dim dv As New DataView()  
  7. Dim drv As DataRowView  
  8. Dim tmpNd As TreeNode  
  9. Dim intId As Integer  
  10. dv.Table = ds.Tables("tree")  
  11. dv.RowFilter = "PARENTID=’" & parentId & "’"  
  12. For Each drv In dv  
  13. tmpNd = New TreeNode()  
  14. strId = drv("NODE_ID")  
  15. tmpNd.ID = strId 
  16. tmpNd.Text = drv("NODE_NAME ")  
  17. tmpNd.ImageUrl = drv("ICON").ToString  
  18. Nds.Add(tmpNd)  
  19. intiTree(Nds(Nds.Count - 1).Nodes, intId)  
  20. Next  
  21. End Sub 

2. add and delete Tree nodes

To Add, delete, or modify a node in a Treeview, you only need to use the Add, Remove, and other methods of the Nodes attribute. It is worth noting that. in NET, the Nodes set of Treeview is different from that in VS6.0. VS6.0 is a large set, while. in. NET, each Node in the hierarchy has the Nodes attribute. The addition, deletion, and modification of Tree nodes are significantly different from those of VS6.0, especially when a tree node is deleted.
Private Sub ButAdd_Click (ByVal sender As System. Object, ByVal e As System. EventArgs) Handles ButAdd. click' Add a subnode under the selected node

 
 
  1. Dim tmpNd As New TreeNode (), NdSel As TreeNode
  2. TmpNd. ID=GetNewId()
  3. NdSel=TreeView1. GetNodeFromIndex (TreeView1.SelectedNodeIndex) 'selected node
  4. TmpNd. Text="New node" 
  5. NdSel. Nodes. Add (tmpNd)
  6. Dim myRow As DataRow
  7. MyRow=Ds. Tables ("tree"). NewRow ()
  8. MyRow ("NODE_NAME") = tmpNd. ID
  9. MyRow ("NODE_DESCRIPT") = "new node" & tmpNd. ID & "_" & NdSel. ID
  10. MyRow ("PARENT_NAME") = NdSel. ID
  11. Ds. Tables ("tree"). Rows. Add (myRow)
  12. End Sub
  13. Private Sub ButDele_Click (ByVal sender As Object, ByVal e As System. EventArgs)
    Handles ButDele. click' Delete the selected node
  14. Dim idxString=TreeView1. SelectedNodeIndex ()
  15. GetNdCol (idx). Remove (TreeView1.GetNodeFromIndex (idx ))
  16. Dim dv As New DataView (), recNo As Integer
  17. Dv. Table=Ds. Tables ("tree ")
  18. Dv. RowFilter="NODEID ="& NdId
  19. Dv. Delete (0)
  20. End Sub
  21. Private Function GetNdCol (ByVal idx As String) As TreeNodeCollection
  22. 'Obtain the Nodes set of the parent node of the selected node.
  23. Dim cnt As Integer, I As Integer
  24. Dim tmpNds As TreeNodeCollection
  25. Dim idxs () As String
  26. Idxs=Split(Idx ,".")
  27. Cnt=UBound(Idxs)
  28. IfCnt=0Then
  29. TmpNds=TreeView1. Nodes
  30. Else
  31. TmpNds=TreeView1. Nodes (CInt (idxs (0). Nodes
  32. ForI=1To cnt-1
  33. TmpNdsTmpNds= TmpNds (CInt (idxs (I). Nodes
  34. Next
  35. End If
  36. Return tmpNds
  37. End Function

3. Modify and move a tree node

Because the server control does not support mouse drag, you cannot drag a node like a Windows program. Here, you can select a parent node. Moving is implemented by deleting the original location and adding a new location. Note that the node information is saved before deletion.

 
 
  1. Private Sub treeviewincluselectedindexchange (ByVal sender As Object,
    ByVal e As Microsoft. Web. UI. WebControls. TreeViewSelectEventArgs)
    Handles TreeView1.SelectedIndexChange
  2. Dim dv As New DataView ()
  3. Dv. Table=Ds. Tables ("tree ")
  4. Dim tmpNdTreeNode=TreeNdSel(E. OldNode), tmpNds As TreeNodeCollection
  5. Dv. RowFilter="NODEID ="& TmpNd. ID
  6. Dv (0) ("NODE_DESCRIPT") = Me. TextBox1.Text
  7. Dv (0) ("ADDRESS") = Me. TextBox2.Text
  8. Dv (0) ("TARGET") = Me. TextBox3.Text
  9. Dv (0) ("ICON") = Me. TextBox4.Text
  10. If dv (0) ("PARENTID"). ToString<>Me. DropDownList1.SelectedItem. Value Then
  11. 'Move a node
  12. Dv (0) ("PARENT_NAME") = Me. DropDownList1.SelectedItem. Value
  13. IfMe. DropDownList1.SelectedItem. Value="ROOT"Then
  14. TmpNds=TreeView1. Nodes
  15. Else
  16. TmpNds=FromIdToNode(Me. DropDownList1.SelectedItem. Value,
    TreeView1.Nodes). Nodes the Nodes set of the new parent node
  17. End If
  18. GetNdCol (e. OldNode). Remove (tmpNd)
  19. TmpNds. Add (tmpNd)
  20. End If
  21. TmpNd. Text=Me. TextBox1.Text
  22. TmpNd. ImageUrl=Me. TextBox4.Text
  23. TmpNd=TreeView1. GetNodeFromIndex (TreeView1.SelectedNodeIndex)
  24. Dv. RowFilter="NODEID ="& TmpNd. ID
  25. Me. TextBox1.Text=Dv(0) ("NODENAME"). ToString
  26. Me. TextBox2.Text=Dv(0) ("ADDRESS"). ToString
  27. Me. TextBox3.Text=Dv(0) ("TARGET"). ToString
  28. Me. TextBox4.Text=Dv(0) ("ICON"). ToString
  29. End Sub
  30. Private Function FromIdToNode (ByVal ID As String,
    ByVal Nds As TreeNodeCollection) As TreeNode
  31. 'Search nodes with keywords
  32. Dim I As Integer
  33. Dim tmpNd As TreeNode, tmpNd1 As TreeNode
  34. For Each tmpNd In Nds
  35. IfTmpNd. ID= ID Then
  36. Return tmpNd
  37. Exit Function
  38. End If
  39. TmpNd1=FromIdToNode(ID, tmpNd. Nodes)
  40. If Not (tmpNd1 Is Nothing) Then
  41. Return tmpNd1
  42. Exit Function
  43. End If
  44. Next
  45. Return Nothing
  46. End Function

Iv. Conclusion

The preceding sections describe how to modify database data while adding, deleting, modifying, and moving Tree nodes. Due to space limitations, the author only introduces the basic ideas, processes and key steps here, and does not list the detailed source code. Readers can complete it on their own. If you need detailed source code, you can contact me. In this article, the program has been debugged under VS. NET, SQLServer, Windows 2000, and IIS5.0.

  1. DataList and Repeater controls of ASP. NET
  2. Analysis of IIS ing in ASP. NET
  3. Overview ASP. NET status types
  4. Introduction to ASP. NET and Web servers
  5. EnableViewState attribute of ASP. NET

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.