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