1. Preliminary use
(1)
Http://msdn.microsoft.com/downloads/samples/internet/default.asp? Url =/Downloads/samples/Internet/ASP_DOT_NET_ServerControls/WebControls/default. asp
(2) No tree display
First, download the package for automatic installation and manual installation.
To download an automatic installation package of about 650 kb.
Second, TreeView requires that the client browser version be IE5.5 or later. It is recommended that the client be upgraded to IE6.0
(3) flashing
If the AutoPostBack attribute is set to true, SelectedIndexChange can be executed. However, the refresh will be amazing. If you do not need to refresh, set the AutoPostBack attribute to FALSE.
(4) several common attributes and Methods
~ Index to obtain the position of a tree node in the tree node set.
~ Nodes gets the set of Tree Nodes allocated to the Tree View control.
~ Parent gets or sets the Parent container of the control.
~ SelectedNode gets or sets the currently selected tree node in the Tree View control.
~ ExpandAll expands all Tree nodes.
~ Checked gets or sets a value to indicate whether the tree node is selected.
~ Text gets or sets the Text displayed in the tree node label.
~ Expand the tree node.
~ Clear
~ Remove removes the current Tree node from the Tree View control.
(5) several common operations: add, delete, modify, and cut
@ Add a node:
Dim tmpNd3 As New Microsoft. Web. UI. WebControls. TreeNode ()
Dim NdSel As New Microsoft. Web. UI. WebControls. TreeNode ()
'Ndsel is the currently selected node, and the new node will be its child node
NdSel = Treepaybasic. GetNodeFromIndex (Treepaybasic. SelectedNodeIndex)
TmpNd3.Text = "add node"
'Add this new node to the tree.
NdSel. Nodes. Add (tmpNd3)
@ Delete a node:
Dim tmpNd3 As New Microsoft. Web. UI. WebControls. TreeNode ()
Dim NdSel As New Microsoft. Web. UI. WebControls. TreeNode ()
'Ndsel is the currently selected node to be deleted, and tmpNd3 is its parent node.
NdSel = Treepaybasic. GetNodeFromIndex (Treepaybasic. SelectedNodeIndex)
If (Treepaybasic. SelectedNodeIndex <> "0") Then
TmpNd3 = NdSel. Parent
TmpNd3.Nodes. Remove (NdSel)
Else
Treepaybasic. Nodes. Clear ()
End If
@ Modify node:
Dim NdSel As New Microsoft. Web. UI. WebControls. TreeNode ()
NdSel = Treepaybasic. GetNodeFromIndex (Treepaybasic. SelectedNodeIndex)
NdSel. Text = "aaa"
@ Cut and paste
Cut:
Dim tmpNd3 As New Microsoft. Web. UI. WebControls. TreeNode ()
Dim NdSel As New Microsoft. Web. UI. WebControls. TreeNode ()
'Ndsel is the currently selected node to be deleted, and tmpNd3 is its parent node.
NdSel = Treepaybasic. GetNodeFromIndex (Treepaybasic. SelectedNodeIndex)
'Save the cut nodes to the session
Session ("node") = NdSel
If (Treepaybasic. SelectedNodeIndex <> "0") Then
TmpNd3 = NdSel. Parent
TmpNd3.Nodes. Remove (NdSel)
End If
Paste:
Dim tmpNd3 As New Microsoft. Web. UI. WebControls. TreeNode ()
Dim NdSel As New Microsoft. Web. UI. WebControls. TreeNode ()
'Ndsel is the parent node of the current node to be pasted
NdSel = Treepaybasic. GetNodeFromIndex (Treepaybasic. SelectedNodeIndex)
TmpNd3 = Session ("node ")
NdSel. Nodes. Add (tmpNd3)
2. algorithm and database design using recursive Spanning Tree
(1) recursive description
The program calls its own programming method called recursion ). In Tree Generation and graph traversal, recursion is often used. Evaluate the classical algorithm n! In (calculate the factorial of n), the recursive method is used. The advantage of recursive algorithms is conciseness and good scalability. But the disadvantages are also obvious: inefficiency. Because recursion means that the program continuously calls itself and consumes a large proportion of system resources. As the number of nodes increases, the execution efficiency becomes very low.
To solve the problem that the layer tree is not fixed during the tree generation process, and to make the tree more scalable. Tree Generation uses recursive methods. Once the code of the Spanning Tree is written, you can generate an infinitely layered tree without modifying the source code. The structure of the tree is completely determined by the data in the table in the database.
(2) Database Design
Create a database, design the tree chart information table Treetable, the table attributes include Nodeid, Parentid, Nodename, Address, and other fields (used to indicate the node ID, parent node ID, node name, and link Address respectively ), other attributes are determined based on actual user requirements and design. 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 ID of the parent node of the current node. For example, two nodes have parent-child relationship, the Parentid value of a child node is the Nodeid of its parent node. The node number and parent-child nodes form a "linked list", which represents and records the hierarchy of nodes on the tree.
The specific table design is as follows: (simplified models are complex in actual use)
Primary Key property name type length can be empty attribute meaning
Yesnodeid int 6 no node ID
Parentid int 6 no parent node ID
Nodename char 50 no node name
Address char 80 URL
Note: The link address is mainly used in the environment where the tree is used in the framework. Links can point to addresses on other frame pages or have different parameters.
(3) program code
――――――――――――
'Spanning tree Functions
Private Sub inittree (ByRef Nds As Microsoft. Web. UI. WebControls. TreeNodeCollection, ByVal parentId As Integer)
Dim dv As New DataView ()
Dim dvrow As DataRowView
Dim tmpNode As Microsoft. Web. UI. WebControls. TreeNode
'Intid is a numeric variable. It records and transmits the ID of the current record as the PARENTID value of its subnode.
Dim intId As Integer
Dv. Table = mySet. Tables ("paybasic ")
'Parentid passes intId in the additem function. The following statement is used to find the child set of the current node.
Dv. RowFilter = "parentID = '" & parentId &"'"
'If the current node has children, traverse all the children and call the recursive function.
For Each dvrow In dv
TmpNode = New Microsoft. Web. UI. WebControls. TreeNode ()
'Assigns a value to each attribute of the current node.
TmpNode. ID = dvrow ("nodeID ")
TmpNode. Text = dvrow ("nodename ")
TmpNode. NavigateUrl = dvrow ("Address ")
IntId = dvrow ("parentID ")
'Add a node
Nds. Add (tmpNode)
'Call recursive functions
Inittree (Nds. Count-1). Nodes, intId)
Next
End Sub
――――――――――――――――――
CreateReaderDataSet ()
Inittree (FIG, 999)
―――――――――――――――――――
'Dataset generation function
Private Sub CreateReaderDataSet ()
'Connect at runtime and set Connection Properties
MyConn = New System. Data. OleDb. OleDbConnection ("Provider = MSDAORA.1; Data Source = oracle9; User ID = user; Password = ****;")
'Set SelectCommand command
MyAdapter. SelectCommand = New System. Data. OleDb. OleDbCommand ("select * from treenode", MyConn)
'Fill the dataset
MyAdapter. Fill (mySet, "treenode ")
End Sub
Database-related node operations:
(1) Add a node
1.1 node tree Addition
// The function of the program is to add a new node under the clicked Node
Dim tmpNd3 As New Microsoft. Web. UI. WebControls. TreeNode ()
Dim NdSel As New Microsoft. Web. UI. WebControls. TreeNode ()
'Ndsel is the currently selected node to be deleted, and tmpNd3 is its parent node.
NdSel = Treepaybasic. GetNodeFromIndex (Treepaybasic. SelectedNodeIndex)
'Attributes of the node to be added
TmpNd3.ID = 111
TmpNd3.Text = "aaa"
'Use nodes. add to add a node
NdSel. Nodes. Add (tmpNd3)
1.2 add nodes in the database
// The database connection statement is omitted here
'Connect at runtime and set Connection Properties
Dim insertcomm = New System. Data. OleDb. OleDbCommand ()
'Define various attributes of the storage command
Insertcomm. commandText = "insert into treebasic (id, parentid, name) values ('" & pid & "', '" & parid & "', '" & nodetext &"')"
Insertcomm. Connection = MyConn
'Open the connection and execute the command
MyConn. Open ()
Insertcomm. ExecuteNonQuery ()
MyConn. Close ()
(2) modify a node
2.1 tree modification of node attributes
Dim tmpNd3 As New Microsoft. Web. UI. WebControls. TreeNode ()
'Tmpnd3 is the currently selected node
TmpNd3 = Treepaybasic. GetNodeFromIndex (Treepaybasic. SelectedNodeIndex)
TmpNd3.Text = "aaa"
2.2 node attribute modification operations in the database
'Define the attributes of the modified command
Dim updatecomm = New System. Data. OleDb. OleDbCommand ()
'Define various attributes of the modify command
Updatecomm. commandText = "update treebasic set name = '" & nodetext & "', remark = '" & remark &"', links = '"& purl &" 'where payid = "& CInt (pid )&""
Updatecomm. Connection = MyConn
'Open the connection and execute the command
MyConn. Open ()
Updatecomm. ExecuteNonQuery ()
MyConn. Close ()
Find the node and expand the example. You can see:
Private Sub findandexpand_Click (ByVal sender As System. Object, ByVal e As System. EventArgs) Handles Button1.Click
'Treepaybasic. Nodes (0) is the heel node of the tree. Start from the root node
If (Treepaybasic. Nodes (0). Text = Me. TextBox1.Text) Then
Treepaybasic. SelectedNodeIndex = "0"
Else
'The node to be searched is not the root node, and recursive function lookup is called
Findnode (Treepaybasic. Nodes)
End If
End Sub
'Query node functions