top
Loading...
ASP.NET2005Treeview終極解決方案
教程推薦
·ASP.NET初學者入門實踐
·Visual Baisc.NET入門
·基于C#的接口基礎教程
·Visual Studio 2005
精彩專題
·ASP.NET創建XML Web服務
·Visual Basic 9.0新功能
·VB2005實現RSS覽盡天下事
主題社區
·ASP.NET源碼·ASP.NET

這幾天在寫HRM的時候 這問題搞了我兩天,開始在使用Google 找了半天都是一堆垃圾,都是使用算法的較多, 后來就去了的msdn.yesky.com 找到點啟示。 好了廢話多說無用。

首先表結構如下 表名 Test


寫個存儲過程 GetTreeview

這個不用我說了吧下面用到

為了速度緩存DataTable

Public Function GetTreeTable() As DataTable
Dim dt As New DataTable()
dt = HttpContext.Current.Cache("Treeview")
If dt Is Nothing Then
Dim Conn As New SqlConnection
Dim clsConnDatabase As New ConnectionDatabase
Conn = clsConnDatabase.ConnDatabase
Dim Command As New SqlCommand
Command.Connection = Conn
Command.CommandText = "GetTreeview"
Command.CommandType = CommandType.StoredProcedure
Command.ExecuteNonQuery()

Dim da As New SqlDataAdapter(Command)

dt = New DataTable()
da.Fill(dt)
HttpContext.Current.Cache.Insert("Treeview", dt)
End If
Return dt
End Function

這里是主要阿

Public Sub PopulateNodes(ByVal nodes As TreeNodeCollection, Optional ByVal intParentID As Int32 = 0)

Dim dt As New DataTable()
dt = clsWebForms.GetTreeTable()
Dim strExpression As String
strExpression = "[parentID] = " & intParentID
Dim foundRows() As DataRow
foundRows = dt.Select(strExpression)

Dim I As Integer
For I = 0 To foundRows.GetUpperBound(0)
Dim tn As New TreeNode()
tn.Text = foundRows(I).Item(“TableName”).ToString()
tn.Value = foundRows(I).Item("ID").ToString()
Dim dr() As DataRow
dr = dt.Select("[parentID] = " & tn.Value)
If dr.GetUpperBound(0) > -1 Then
tn.PopulateOnDemand = True
End If
nodes.Add(tn)
Next
End Sub

建立WebForm 放入Treeview

Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

If Not Page.IsPostBack Then
PopulateNodes(TreeView1.Nodes, 0)
End If
End Sub

Protected Sub TreeView1_TreeNodePopulate(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.TreeNodeEventArgs) Handles TreeView1.TreeNodePopulate

PopulateNodes(e.Node.ChildNodes, e.Node.Value)
End Sub

至于速度我沒測試,如果大家有興趣幫忙測測。

軟件頻道精品推薦

更多精彩
點擊體驗>>

作者:http://www.zhujiangroad.com
來源:http://www.zhujiangroad.com
北斗有巢氏 有巢氏北斗