ASP.net 2005 Treeview 无限分类非地递归终极解决方案

这几天在写HRM的时候 这问题搞了我两天,哈哈!开始在使用Google 找了半天都是一堆垃圾,都是使用算法的较多, 后来就去了www.asp.net 找到点启示。

好了废话多说无用。

首先表结构如下 表名 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

至于速度我没测试,如果大家有兴趣帮忙测测。

 

题外话(突然发现自己老了,26岁了写了快7年的程序了 一直使用VB各种版本,   MasmMS宏汇编语言)在VB 6.0 时代 2000 年左右建立了 VB 编程为主的个人主页编程浪子) by Shadow

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 3
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值