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.
- Private Sub CreateDataSet () 'create a dataset
- Dim myConn As New SqlConnection ()
- Dim myCmd As New SqlCommand ("select NODEID, NODENAME, PARENTID,
ADDRESS, ICON from Tree_info ", myConn)
- Dim myDataAdapter As New SqlDataAdapter ()
- MyConn. ConnectionString=Application("Connectstring ")
- MyCmd. CommandText=""
- MyCmd. Connection=MyConn
- MyDataAdapter. SelectCommand=MyCmd
- MyDataAdapter. Fill (ds, "tree ")
- End Sub
The basic idea of building a tree is to recursively call the display subtree from the root node.
- Private Sub Page_Load(ByVal sender As System.Object,
ByVal e As System.EventArgs) Handles MyBase.Load
- CreateDataSet()
- intiTree(TreeView1.Nodes, 0)
- End Sub
- Private Sub intiTree(ByRef Nds As TreeNodeCollection,
ByVal parentId As Integer)
- Dim dv As New DataView()
- Dim drv As DataRowView
- Dim tmpNd As TreeNode
- Dim intId As Integer
- dv.Table = ds.Tables("tree")
- dv.RowFilter = "PARENTID=’" & parentId & "’"
- For Each drv In dv
- tmpNd = New TreeNode()
- strId = drv("NODE_ID")
- tmpNd.ID = strId
- tmpNd.Text = drv("NODE_NAME ")
- tmpNd.ImageUrl = drv("ICON").ToString
- Nds.Add(tmpNd)
- intiTree(Nds(Nds.Count - 1).Nodes, intId)
- Next
- 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
- Dim tmpNd As New TreeNode (), NdSel As TreeNode
- TmpNd. ID=GetNewId()
- NdSel=TreeView1. GetNodeFromIndex (TreeView1.SelectedNodeIndex) 'selected node
- TmpNd. Text="New node"
- NdSel. Nodes. Add (tmpNd)
- Dim myRow As DataRow
- MyRow=Ds. Tables ("tree"). NewRow ()
- MyRow ("NODE_NAME") = tmpNd. ID
- MyRow ("NODE_DESCRIPT") = "new node" & 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' Delete the selected node
- Dim idxString=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
- 'Obtain the Nodes set of the parent node of the selected node.
- Dim cnt As Integer, I As Integer
- Dim tmpNds As TreeNodeCollection
- Dim idxs () As String
- Idxs=Split(Idx ,".")
- Cnt=UBound(Idxs)
- IfCnt=0Then
- TmpNds=TreeView1. Nodes
- Else
- TmpNds=TreeView1. Nodes (CInt (idxs (0). Nodes
- ForI=1To cnt-1
- TmpNdsTmpNds= TmpNds (CInt (idxs (I). Nodes
- Next
- End If
- Return tmpNds
- 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.
- Private Sub treeviewincluselectedindexchange (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 tmpNdTreeNode=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
- 'Move a node
- Dv (0) ("PARENT_NAME") = Me. DropDownList1.SelectedItem. Value
- IfMe. DropDownList1.SelectedItem. Value="ROOT"Then
- TmpNds=TreeView1. Nodes
- Else
- TmpNds=FromIdToNode(Me. DropDownList1.SelectedItem. Value,
TreeView1.Nodes). Nodes the Nodes set of the new parent node
- 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
- 'Search nodes with keywords
- Dim I As Integer
- Dim tmpNd As TreeNode, tmpNd1 As TreeNode
- For Each tmpNd In Nds
- IfTmpNd. 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
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.
- DataList and Repeater controls of ASP. NET
- Analysis of IIS ing in ASP. NET
- Overview ASP. NET status types
- Introduction to ASP. NET and Web servers
- EnableViewState attribute of ASP. NET