ASP.net 2005 Treeview 無限分類非地遞迴終極解決方案

來源:互聯網
上載者:User
asp.net|treeview|遞迴|解決

為了速度緩衝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

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

 

題外話(突然發現自己老了,26歲了寫了快7年的程式了 一直使用VB各種版本, 及  Masm(MS宏組合語言)在VB 6.0 時代 2000 年左右建立了 以VB 編程為主的個人首頁編程浪子) by Shadow

註:本文轉自http://blog.csdn.net/shadowsky/archive/2006/06/18/811461.aspx

這幾天在寫HRM的時候 這問題搞了我兩天,哈哈!開始在使用Google 找了半天都是一堆垃圾,都是使用演算法的較多, 後來就去了www.asp.net 找到點啟示。好了廢話多說無用。首先表結構如下 表名 Test寫個預存程序 GetTreeview這個不用我說了吧下面用到  

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.