這篇文章給大家介紹ASP.NET樹形圖是怎樣的,內(nèi)容非常詳細(xì),感興趣的小伙伴們可以參考借鑒,希望對(duì)大家能有所幫助。
創(chuàng)新互聯(lián)致力于互聯(lián)網(wǎng)品牌建設(shè)與網(wǎng)絡(luò)營(yíng)銷,包括成都網(wǎng)站建設(shè)、網(wǎng)站制作、SEO優(yōu)化、網(wǎng)絡(luò)推廣、整站優(yōu)化營(yíng)銷策劃推廣、電子商務(wù)、移動(dòng)互聯(lián)網(wǎng)營(yíng)銷等。創(chuàng)新互聯(lián)為不同類型的客戶提供良好的互聯(lián)網(wǎng)應(yīng)用定制及解決方案,創(chuàng)新互聯(lián)核心團(tuán)隊(duì)十載專注互聯(lián)網(wǎng)開發(fā),積累了豐富的網(wǎng)站經(jīng)驗(yàn),為廣大企業(yè)客戶提供一站式企業(yè)網(wǎng)站建設(shè)服務(wù),在網(wǎng)站建設(shè)行業(yè)內(nèi)樹立了良好口碑。
ASP.NET樹形圖用于顯示按照樹形結(jié)構(gòu)進(jìn)行組織的數(shù)據(jù),其用途比較廣泛,如計(jì)算機(jī)中的文件系統(tǒng)(Windows中的資源管理器)、企業(yè)或公司的組成結(jié)構(gòu)等。我們知道在Windows下VB、PB、Delphi等工具提供了一個(gè)功能很強(qiáng)的樹型控件TreeView,利用Treeview控件可以方便地開發(fā)樹形圖。然而在網(wǎng)頁(yè)上實(shí)現(xiàn)樹形圖就不那么容易了,現(xiàn)在在ASP.NET中利用微軟提供的Internet Explorer WebControls它使得網(wǎng)頁(yè)上的樹形圖開發(fā)與在Windows下一樣的方便,一樣的功能強(qiáng)大,甚至更靈活。
介紹用Internet Explorer WebControls開發(fā)ASP.NET樹形圖的方法,由于樹形圖結(jié)構(gòu)較復(fù)雜,使用起來(lái)常不知如何下手。筆者結(jié)合最近剛為公司用ASP.NET編寫的應(yīng)用程序管理器這一具體實(shí)例,詳細(xì)闡述在ASP.NET下如何將Internet Explorer WebControls的使用與數(shù)據(jù)庫(kù)聯(lián)系起來(lái),實(shí)現(xiàn)數(shù)據(jù)分任意多層顯示,方便地進(jìn)行增加、修改、刪除、移動(dòng)操作。筆者希望通過對(duì)該實(shí)例的闡述,達(dá)到拋磚引玉的效果,與各位同仁相互交流,共同進(jìn)步。
一、樹的建立
具體方法是:創(chuàng)建一個(gè)數(shù)據(jù)庫(kù),設(shè)計(jì)樹圖信息表TREE_INFO,包含NODEID、PARENTID、NODENAME、ADDERSS、ICON字段,其它字段根據(jù)實(shí)際業(yè)務(wù)而定,節(jié)點(diǎn)名稱NODENAME將在樹型控件的節(jié)點(diǎn)上顯示,NODEID字段保存節(jié)點(diǎn)的***標(biāo)識(shí)號(hào),PARENTID表示當(dāng)前節(jié)點(diǎn)的父節(jié)點(diǎn)號(hào),標(biāo)識(shí)號(hào)組成了一個(gè)“鏈表”,記錄了樹上節(jié)點(diǎn)的結(jié)構(gòu)。設(shè)計(jì)一個(gè)Web窗體其上放置TreeView控件。
Private Sub CreateDataSet()’建立數(shù)據(jù)集
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
建樹的基本思路是:從根節(jié)點(diǎn)開始遞歸調(diào)用顯示子樹
Private Sub Page_Load(ByVal sender As System.Object,
ByVal e As System.EventArgs) Handles MyBase.LoadCreateDataSet()
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
二、增加、刪除樹節(jié)點(diǎn)
單純?cè)赥reeview 上增加、刪除、修改節(jié)點(diǎn)只需用Nodes屬性的Add、 Remove、等方法即可,值得注意的地方是VS.NET中Treeview的Nodes集合與VS6.0中的區(qū)別,VS6.0中的是一個(gè)大的集合,而 VS.NET中的是分層的每個(gè)Node下都有Nodes屬性。增加、刪除、修改樹節(jié)點(diǎn)時(shí)與VS6.0相比有很大差別,特別是刪除時(shí)。
Private Sub ButAdd_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ButAdd.Click’在選定的節(jié)點(diǎn)下添加子節(jié)點(diǎn)
Dim tmpNd As New TreeNode(), NdSel As TreeNode
tmpNd.ID = GetNewId()
NdSel = TreeView1.GetNodeFromIndex(TreeView1.SelectedNodeIndex)’選中的節(jié)點(diǎn)
tmpNd.Text = "新節(jié)點(diǎn)"
NdSel.Nodes.Add(tmpNd)
Dim myRow As DataRow
myRow = ds.Tables("tree").NewRow()
myRow("NODE_NAME") = tmpNd.ID
myRow("NODE_DESCRIPT") = "新節(jié)點(diǎn)" & 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’刪除選中的節(jié)點(diǎn)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
‘獲得選中節(jié)點(diǎn)的父節(jié)點(diǎn)的Nodes集合
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
三、修改、移動(dòng)樹節(jié)點(diǎn)
由于服務(wù)器控件不支持鼠標(biāo)拖動(dòng)事件,所以不能象Windows程序那樣通過拖動(dòng)移動(dòng)節(jié)點(diǎn),這里是通過選擇父節(jié)點(diǎn)的方式。移動(dòng)是通過在原位置刪除,新位置添加實(shí)現(xiàn)的,要注意在刪除時(shí)先保存節(jié)點(diǎn)信息。
Private Sub TreeView1_SelectedIndExchange(ByVal sender As Object,
ByVal e As Microsoft.Web.UI.WebControls.TreeViewSelectEventArgs)
Handles TreeView1.SelectedIndexChangeDim 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
‘移動(dòng)節(jié)點(diǎn)
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’新的父節(jié)點(diǎn)的Nodes集合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‘由關(guān)鍵字查找節(jié)點(diǎn)
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
以上闡述ASP.NET樹形圖,以及如何在對(duì)樹節(jié)點(diǎn)進(jìn)行維護(hù)(增加、刪除、修改、移動(dòng))的同時(shí),修改數(shù)據(jù)庫(kù)數(shù)據(jù)。由于篇幅所限,筆者在此只對(duì)基本思路和流程及關(guān)鍵步驟作了介紹,并未列出詳細(xì)源代碼,讀者可自行完善。需要詳細(xì)源代碼者可與我聯(lián)系,本文程序在VS.NET、SQLServer、 Windows 2000、IIS5.0下調(diào)試通過。
關(guān)于ASP.NET樹形圖是怎樣的就分享到這里了,希望以上內(nèi)容可以對(duì)大家有一定的幫助,可以學(xué)到更多知識(shí)。如果覺得文章不錯(cuò),可以把它分享出去讓更多的人看到。