asp.net 2.0 中递归树的实现

80酷酷网    80kuku.com

  asp.net|递归

HTML页面:

 

--------------------------------------------------------------------------------

<%... Page Language="VB" AutoEventWireup="false" CodeFile="TreeView.aspx.vb" Inherits="TreeView" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
    <title>无标题页</title>
    <BASE target="main">
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <asp:TreeView ID="TreeView1" runat="server" ExpandDepth="1" Font-Size="9pt" AutoGenerateDataBindings="False" EnableTheming="True" NodeWrap="True" ShowLines="True" Target="_blank">
        </asp:TreeView>
   
    </div>
    </form>
</body>
</html>
 后台CS代码:

--------------------------------------------------------------------------------

Imports System.Web
Imports System.Data.SqlClient
Imports System.Data

Partial Class TreeViewClass TreeView
    Inherits System.Web.UI.Page

    Dim ds As DataSet = New System.Data.DataSet()

    Protected Sub Page_Load()Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        If Not IsPostBack Then
            Dim sqlstr As String = "select a.*,T32003.T3200303 as orderProgramID from(select 项目代码 as nodeID,项目名称 as nodeName,项目名称 as parentID from acf_v_lefttree_Admin where 项目代码<>' ' and 项目代码 is not null group by 项目代码,项目名称 union all select 系统代码 as nodeID,系统名称 as nodeName,项目代码 as parentID from acf_v_lefttree_Admin where 系统代码<>' ' and 系统代码 is not null group by 项目代码,系统代码,系统名称  union all select 子系统代码 as nodeID,子系统名称 as nodeName,系统代码 as parentID from acf_v_lefttree_Admin where 子系统代码<>' ' and 子系统代码 is not null group by 系统代码,子系统代码,子系统名称  union all select 程式代码 as nodeID,程式名称 as nodeName,子系统代码 as parentID from acf_v_lefttree_Admin where  程式代码<>' ' and 程式代码 is not null group by 子系统代码,程式代码,程式名称 ) a left outer join T32003 on a.nodeID = T32003.T3200301"
            Dim conn As SqlConnection = New SqlConnection("server=(local);database=acfv3;uid=sa;pwd=888888")
            Dim sqlada As SqlDataAdapter = New SqlDataAdapter(sqlstr, conn)
            sqlada.Fill(ds, "TreeTb")
            createtree(TreeView1.Nodes, "我的ACF")

        End If
    End Sub

    Private Sub createtree()Sub createtree(ByVal TreeVwNds As TreeNodeCollection, ByVal strCurrentID As String)
        Dim datviw As New DataView
        Dim datrow As DataRowView
        Dim TreeVwNode As TreeNode
        Dim currentId As String
        datviw.Table = ds.Tables("TreeTb")
        datviw.Sort = "orderProgramID"
        Try
            datviw.RowFilter = " parentID ='" & strCurrentID & "'"
            If datviw.Count > 0 Then
                For Each datrow In datviw
                    TreeVwNode = New TreeNode
                    TreeVwNode.Value = datrow("nodeID")
                    currentId = datrow("nodeID")
                    TreeVwNds.Add(TreeVwNode)
                    TreeVwNode.Text = Trim(datrow("nodeName"))
                    TreeVwNode.Target = "_blank"
                    Dim i As Integer = TreeVwNds.Count
                    createtree(TreeVwNds(TreeVwNds.Count - 1).ChildNodes, currentId)
                Next
            End If
        Catch ex As Exception
            Response.Write(ex.ToString)

        End Try

    End Sub
End Class

分享到
  • 微信分享
  • 新浪微博
  • QQ好友
  • QQ空间
点击: