Asp. The realization of tree graph in net

Source: Internet
Author: User
Tags exit file system integer tostring
asp.net tree is used to display data that is organized according to a tree structure, such as a file system in a computer (a resource manager in Windows), a corporate or corporate structure, and so on. We know that under Windows VB, PB, Delphi and other tools provide a very powerful tree control TreeView, using the TreeView control can easily develop a tree diagram. However, it's not easy to implement tree on a Web page, and now use Microsoft's Internet Explorer WebControls in ASP.net, which makes tree development on Web pages as convenient, powerful, and even flexible as it is under Windows.


This paper introduces the method of developing tree using Internet Explorer WebControls, because the tree structure is more complicated, so it is often not known how to start. The author combined with the recent application manager for the company to write a specific example, the detailed description of the Internet Explorer in the asp.net under the use of WebControls with the database, the realization of arbitrary multi-level data display, easy to increase, Modify, delete, move operations. I hope that through the elaboration of this example to achieve the effect of a good, and colleagues to communicate with each other, and common progress.


Internet Explorer WebControls is not in Vs.net's standard server control and will be downloaded at Microsoft's site by downloading the address:

Http://msdn.microsoft.com/downloads/samples/internet/default.asp?url=/Downloads/samples/Internet/ASP_DOT_NET_ Servercontrols/webcontrols/default.asp

For the first time after installation, right-click the Toolbox customize Toolbox ... Micosoft.Web.UI.WebControls.Treeview is selected after the →.net Framework components is found, so the TreeView control appears in the Toolbox.


First, the establishment of trees


The specific method is: Create a database, design tree diagram information table tree_info, including Nodeid, ParentID, nodename, Adderss, icon fields, other fields based on the actual business, the node name nodename will be displayed on the tree control node , the Nodeid field holds a unique identification number for the node, ParentID represents the parent node number of the current node, and the identification number consists of a "linked list" that records the structure of the nodes on the tree. Design a Web form on which to place the TreeView control.

Private Sub CreateDataSet () ' Set up data set

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 achievement is: To start a recursive call from the root node to display the subtree

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


Add and Delete tree nodes


Simply add, delete, and modify nodes in the TreeView only with the Add, remove, and other methods of the nodes attribute, What is noteworthy is the difference between the nodes set of the TreeView in the Vs.net and the VS6.0, the VS6.0 is a large set, and vs.net is the nodes attribute under each node in the hierarchy. Adding, deleting, and modifying tree nodes are very different than VS6.0, especially when you delete them.

Private Sub Butadd_click (ByVal sender as System.Object, ByVal e as System.EventArgs) Handles butadd.click ' Add child nodes under 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 selected node

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

' Gets the nodes collection 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)

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


Third, modify, Move tree node


Because the server control does not support mouse drag events, you cannot drag the mobile node in the same way as a Windows program, by selecting the parent node. The move is done by deleting it in the original location and adding it to the new location, so be careful to save the node information before deleting it.

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

' Move the node

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 ' The Nodes collection 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

' Lookup node by keyword

Dim I as Integer

Dim TMPND as TreeNode, tmpNd1 as TreeNode

For all tmpnd in Nds

If tmpnd.id = ID Then

Return TMPND

Exit Function

End If

TMPND1 = Fromidtonode (ID, Tmpnd.nodes)

If not (TmpNd1 are nothing) Then

Return TMPND1

Exit Function

End If

Next

Return Nothing

End Function


Iv. concluding remarks


The above describes the basic method of tree-like display in asp.net, and how to modify the database data while maintaining the tree nodes (adding, deleting, modifying, moving). Due to space limitations, the author here only the basic ideas and procedures and key steps are introduced, not listed in detail the source code, the reader can be perfected. Need detailed source code can contact me, this program in vs.net, SQL Server, Windows 2000, IIS5.0 debugging pass.


Related Article

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.