Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What does the ASP.NET tree look like?

2025-02-23 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

This article introduces ASP.NET tree diagram is how, the content is very detailed, interested in small partners can refer to reference, hope to be helpful to everyone.

ASP.NET tree diagram is used to display data organized according to tree structure, which is widely used, such as file system in computer (resource manager in Windows), composition structure of enterprise or company, etc. We know that VB, PB, Delphi and other tools under Windows provide a tree control TreeView with strong functions. TreeView control can be used to develop tree diagram conveniently. However, it is not so easy to implement tree diagram on the web page. Now in ASP.NET, Internet Explorer Web Controls provided by Microsoft makes it as convenient, powerful and even more flexible as the tree diagram development on the web page under Windows.

This paper introduces the method of developing ASP.NET tree diagram with Internet Explorer Web Controls. Because of the complexity of tree diagram structure, we often don't know how to use it. Combined with the concrete example of application program manager written recently for company with ASP.NET, the author expounds in detail how to connect the use of Internet Explorer Web Controls with database under ASP.NET, realize the arbitrary multi-layer display of data, and conveniently carry out the operation of adding, modifying, deleting and moving. The author hopes that through the elaboration of this example, to achieve the effect of throwing bricks to attract jade, and colleagues to communicate with each other, common progress.

1. Establishment of trees

The specific methods are: Create a database, design tree graph information table TREE_INFO, including NODEID, PARENTID, NODENAME, ADDENSS, ICON fields, other fields are determined according to the actual business, node name NODENAME will be displayed on the nodes of the tree control, NODEID field stores the *** identification number of the node, PARENTID represents the parent node number of the current node, and the identification numbers form a "linked list", which records the structure of the nodes on the tree. Design a Web form on which to place the TreeView control.

Private Sub CreateDataSet()'Create 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

II. Adding and deleting tree nodes

Simply add, delete, modify nodes on Treeview only need to use the Add, Remove, and other methods of the Nodes attribute, it is worth noting that the Treeview Nodes collection in VS. NET is different from VS6.0, VS6.0 is a large collection, and VS. NET is hierarchical Each Node has Nodes attributes. Adding, deleting, and modifying tree nodes are very different from VS6.0, especially when deleting.

Private Sub ButAdd_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ButAdd.Click'Adds a child node 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 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

'Get 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

tmpNdstmpNds = tmpNds(CInt(idxs(i))).Nodes

Next

End If

Return tmpNds

End Function

III. Modify and move tree nodes

Because server controls do not support mouse drag events, you cannot move nodes by dragging as Windows programs do, here by selecting parent nodes. Move is achieved by deleting at the original position and adding at the new position. Note that the node information should be saved first when deleting.

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

'Mobile 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

'Find nodes by keyword

Dim i As Integer

Dim tmpNd As TreeNode, tmpNd1 As TreeNode

For Each tmpNd In Nds

If tmpNd.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

The above describes ASP.NET tree diagrams and how to modify database data while maintaining tree nodes (adding, deleting, modifying, moving). Due to space constraints, the author here only introduces the basic ideas and processes and key steps, does not list the detailed source code, readers can improve themselves. Need detailed source code can contact me, this program in VS. NET, SQL Server, Windows 2000, IIS5.0 debugging through.

ASP.NET tree diagram on how to share here, I hope the above content can be of some help to everyone, you can learn more knowledge. If you think the article is good, you can share it so that more people can see it.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report