TreeView increases, deletes, modify, traverses operation 1. Use
(1) Download address http://msdn.microsoft.com/downloads/samples/internet/default.asp?url=/downloads/samples/internet/ASP_DOT_NET_SERVERCONTROLS/WebControls/default.asp
(2) Do not show the problem of tree type First: Download the package automatically installed and manually installs two packages. To download the automatic installation package of about 650K. Second: TreeView requires client browser version of IE5.5 and above, it is best to request client upgrade to IE6.0
(3) About flashing the AutoPostBack property to true, SELECTEDEXCHANGE can be executed. However, this is very powerful. If you don't refresh, set the AutoPostBack property to False.
(4) Several properties and methods of commonly used properties and methods ~ INDEX get the position of the tree node in the tree node collection. ~ Nodes Get the tree node collection assigned to the tree view control. ~ Parent gets or sets the parent container for the control. ~ SelectedNode Gets or sets the tree node currently selected in the tree view control. ~ ExpandALL Expands all tree nodes. ~ Checked gets or sets a value to indicate if the tree node is selected. ~ Text Gets or sets the text displayed in the tree node tag. ~ Expand Expand the tree node. ~ CLEAR Clear Tree ~ Remove removes the current tree node from the tree view control.
(5) Commonly used operations: increase, delete, modify, cut
@ 加加 节: DIM TMPND3 AS New Microsoft.Web.ui.WebControls.treenode () Dim Ndsel As New Microsoft.Web.ui.WebControls.treenode () 'NDSEL For the currently selected node, the new node will be its child Node NDSEL = TreePaybasic.GetnodeFromindex (TreePayBasic.SelectedNodeIndex) Tmpnd3.text = "New Node" 'Add this new node in the tree NDSEL.NODES.ADD (TMPND3) @ delete node: DIM TMPND3 AS New Microsoft.Web.ui. WebControls.TreeNode () Dim NdSel as New Microsoft.Web.UI.WebControls.TreeNode () 'NdSel for the currently selected node to be deleted, tmpNd3 as its parent node NdSel = Treepaybasic.GetNodeFromIndex (Treepaybasic.SelectedNodeIndex) If (Treepaybasic .SelectedNodeIndex <> "0") THEN TMPND3 = ndsel.parent tmpnd3.nodes.remove (ndsel) Else Treepaybasic.nodes.cle () 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 For the currently selected to delete nodes, TMPND3 is its parent node NdSel = Treepaybasic.GetNodeFromIndex (Treepaybasic.SelectedNodeIndex) 'is stored in the node sheared 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 for the parent ndsel = treiPaybasic for the current paste node .GetnodeFromindex (TreePayBasic.SelectedNodeIndex) TMPND3 = session ("Node") NDSEL.NODES.ADD (TMPND3) 2. Design of algorithm and database design with recursive spanning trees (1) Recursive description program calls itself is called recursive (Recursion) ). In the generation of the tree and the traversal of the map, there are many of the hands. The classic algorithm is n! (See the step by N), it is a recursive method. The advantages of the recursive algorithm are simple and expandable. But the disadvantage is also obvious: inefficient. Because recursive is that the program continues to call itself, it is relatively large for the resource consumption of the system. As the node increases, the execution efficiency will become very low. In order to solve the problem with the laminar tree during the generation process, it is also to make the tree's scalability better. The generation of the tree uses a recursive method. Once the code of the spanning tree is written, the source code can be generated, generating an unlimited level of the tree. The structure of the tree is completely determined by the data in the database. (2) Database design Create a database, design the tree graph information table Treetable, the property contains NodeID, ParentID, NodeName, Address, and other fields (used to represent nodes, parent node ID, node name, link address), others The property is determined according to the actual user needs and design. Node Nodename will display on the node of the tree control, the NodeID field saves the unique identification number of the node, and the ParentId represents the current node's parent ID number (for example, there are two nodes are the father and child relationship, the child's node's ParentID value is its parent node NodeID), the node number, the child, the parent, forms a "linked list", characterizes and records the hierarchy of the tree node. The table is designated as follows: (Simplified model, actually useful to be complex) Primary key attribute name type length can be expressed in nodeid INT 6 No Node ID ParentID INT 6 No Parent Node ID NodeName Char 50 No Node Name Address Char 80 can be linked address
Remarks: The link address is mainly used in: the environment used in the frame in the frame. The link can point to the address in other framepages or with different parameters.
(3) Program code ------------ Remissive function ------------ 'Spanned Tree Function 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 numerical variable, whose role is to record the current record and the delivery ID, do The ParentID value DIM intid as integer DV.table = mySet.tables ("Paybasic") 'ParentID is passed by the additem function. The role of the following statement is to find the child collection of the current node. DV.rowfilter = "ParentID = '" & ParentID & "'" If the current node has children, traverse all children and call the recursive function. FOR Each DVROW IN DV TMPNODE = New Microsoft.Web.ui.WebControls.treenode () 'assigns values for each attribute for 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 Recurrent function inittree (nds (nds.count - 1) .nodes, intid) Nextend Sub ---------------- Call recursive function ------------ ------ CreateReaderDataSet () inittree (TreePaybasic.Nodes, 999) ----------------- Generate Data Set ------------- ------ 'Generate the function private sub createReaderDataSet ()' Connects at runtime and sets the connection properties myconn = new system.data.oledb.oledbconnection ("provider = msdara.1; data source = oracle9; User ID = user; password = ****; ") Sets selectcommand command myadapter.selectcommand = new system.data.oledb.oledbcommand (" Select * from treenode ", myconn) fill data set myadapter.fill (MySet, "TREENODE") End Sub and database-related node operation: (1) Add Node 1.1 Node Tree Add // Program The function is to add a new node Dim Tmpnd3 as new Microsoft.Web.ui.WebControls under the click node. Treenode () Dim Ndsel As New Microsoft.Web.ui.WebContro Ls.treenode () 'NDSEL is the currently selected to delete nodes, TMPND3 is its parent ndsel = TreePayBasic.GetnodeFromIndex (TreePayBasic.SelectedNodeIndex) to add the nodes to add the nodes TMPND3.ID = 111 TMPND3.TEXT = " AAA "'' Use Nodes.add Add Node NDSEL.NODES.ADD (TMPND3) 1.2 Node Adding Action // Database Connection Statement in the Database Skept Skeleton This omitted 'is running at runtime and sets the connection properties DIM INSERTCOMM = New System.Data. OLEDB.OLDBCOMMAND () 'Defines the various properties of the store command insertComm.commandtext = "Insert Into Treebasic (ID, ParentID, Name) Values ('"
& PID & "','" & Parid & "' 2) modify the node tree node attributes 2.1 modify Dim tmpNd3 as New Microsoft.Web.UI.WebControls.TreeNode () 'tmpNd3 for the currently selected node tmpNd3 = Treepaybasic.GetNodeFromIndex (Treepaybasic.SelectedNodeIndex) tmpNd3.Text = "aaa "2.2 Node Properties in Database Database" Defines the Properties of Modify Command DIM UpdateComm = New System.Data.OleDb.Data.OleDb.Data.OleDb () Defines Various Properties of Modify Command UpdateComm.comMandtext = "Update Treebasic Set Name = '" & nodeText & "', Remark ='" & Remark & ", Links = '" & PURL & "' WHERE PayId =" & CINT (PID) & "UpdateComm.connection = MyConn 'opens the connection, execute the command myconn.open UpdateComm.executenonQuery () MyConn.close () Find an example of node and expands, let's take a look: Private Sub FindAndexpand_Click (Byvale AS System.Object, ByVal E AS System.EventArgs) Handles button1.click
'TreePayBasic.nodes (0) Found IF from the root node (TreePayBasic.Nodes (0) .Text = Me.TextBox1.text) TreePayBasic.SelectedNodeIndeIndex = "0" Else' The node to find is not root, call the recursive function to find findnode (Treepaybasic.Nodes) End If End Sub collection Dim 'find node function Sub findnode (ByRef Nds As Microsoft.Web.UI.WebControls.TreeNodeCollection)' definition of a node tmpNd3 As New Microsoft. Web.ui.WebControls.treenode () 'Find for Each Tmpnd3 in nds' in all children of the current node If the current node has children, then call the recursive function to continue to find if Tmpnd3.nodes.count <> 0 THEN FINDNODE ( Tmpnd3.nodes) Else 'If it is already a leaf node, then a comparison value, if it is the node to find. Node's index if (tmpnd3.text = textbox1.text) THEN DIM INDEX1 AS STRING = Tmpnd3.GetnodeIndex () call expansion node function, and select this node expand (index1) end if end if next end sub 'expanded node Function, and select Node Sub Expand (ByRef Index As String) Dim i as an inTeger 'node in the n-1, expand N-1 time. The node can be opened once in the second layer. For i = 1 to index.replace (".", ") .Length - 1 dim Tmpnd3 as new microsoft.web.ui.webcontrols.treenode () TMPND3 = TreePaybasic.GetnodeFromindex (INDEX, 2 * i - 1 )) TMPND3.EXPANDED = TRUE Next 'Select Node TreePayBasic.SelectedNodeIndex = index end Sub Try not to write in the Treenode_SelectedIndexchange event. It is recommended to use a frame connection. The following gives an example, click the node, and the details of the secondary node are displayed:
(1) Get the variable by the link address: nodeId = me.Request.QueryString ("NodeID")
(2) Query data available in this variable
'Connect at runtime and set the connection attribute myconn = new system.data.oledb.oledbconnection ("provider = msdara.1; data source = oracle; user ID = user; password = user;")' Define Select Conditions Strings DIM STRSELECT AS STRING 'Instantiates Select Conditions Strings and removes all data in the data table you want to find. strselect = "select * from tablebasic where id = '" & nodeid & "'" myAdapter.SelectCommand = New System.Data.OleDb.OleDbCommand (strselect, MyConn) 'filled data set myAdapter.Fill (mySet, "treenode")' Specifies the data source me.dataGridNode.DataSource = Me.MySet.Tables ("TREENODE") 'data binding me.dataGridNode.Database () (3) Setting the data table. Display the relevant field.
After clicking the node, display the data related to the node: The program structure is using the frame, the tree is displayed on the left, and the information related to the tree node is displayed on the right. A Page: Private Sub Page_Load (Byval E AS System.Event, Byval E AS System.EventArgs) Handles MyBase.Load 'Play Initialization Page here User Code IF Page.ispostback = false1 cretereaderDataSet () call spangeled tree Function, generate salary standard trees. Where TreePayBasic is the ID, 999 is the parent ID of the root node (in fact, there is no existence) inittree (TreePayBasic.Nodes, 999) End if End sub 'generated a tree function private subinittree (byref nds as Microsoft.Web.ui. WebControls.TreeNodeCollection, ByVal parentId as Integer) Dim dv as New DataView () Dim drv as DataRowView Dim tmpNd as Microsoft.Web.UI.WebControls.TreeNode 'intId numerical variable, whose role is to record the current record and the delivery ID, As the ParentID value DIM INTID AS INTEGER DV.TABLE = MySet.Tables ("Paybasic") 'ParentID passes the intidID in the AddITEM function. The role of the following statement is to find the child collection of the current node. DV.rowfilter = "ParentID = '" & ParentID & "'" If the current node has children, traverse all children and call the recursive function. For Each DRV IN DV TMPND = New Microsoft.Web.ui.WebControls.treenode () 'assigns values for each attribute for the current node. TMPND.ID = DRV ("PayID") TMPND.TEXT = DRV ("Name") TMPND.NAVIGATEURL = ◎ ◎ ◎ ◎ ##, and add a link intid = DRV ("payid") to add one as needed The node, then find its child nds.add (tmpnd) 'is equivalent to the nested of the function. INITTREE (nds (nds.count - 1) .nodes, intid) Next End Sub
----------------------------- page: Request.Params ("ID") After obtaining the parameters, it is judged.