[转]asp/asp.net中遍历树型结构

数据库:SqlServer2000
:tree
表结构


测试数据


asp代码:
<%@ Language = VBScript%>
<%Option Explicit%>
<%
Dim oConn, oRs, sSql
Dim aTree
Call FillArrayFromRs()
Call blTreeArray(0)

Sub FillArrayFromRs()
    
Dim s_TableName
    s_TableName 
= "tree"
    
Call DBConnBegin()
    
Set oRs = Server.CreateObject( "ADODB.Recordset" )
    sSql 
= "select Id,PowerName,Layer,ParentId from " & s_TableName
    oRs.Open sSql, oConn, 
01
    aTree 
= oRs.GetRows()
    
'response.write ubound(aTree, 2)
    oRs.Close
    
Call DBConnEnd()
End Sub

Sub blTreeArray(n_ParentId)
    
Dim row, rows
    
Dim n_NextParentId
    
Dim n_Space
    rows 
= UBound(aTree, 2)
    
for row = 0 To rows
        
If aTree(3, row) = n_ParentId Then
            
for n_Space = 1 To aTree(2, row)
                Response.Write 
"&nbsp;&nbsp;&nbsp;&nbsp;"
            
Next
            Response.Write aTree(
1, row) & "<br>"
            n_NextParentId 
= aTree(0, row)
            blTreeArray(n_NextParentId)
        
End If
    
Next
End Sub


Sub DBConnBegin()
    
' 如果数据库对象已打开,不要再打开
    If IsObject(oConn) = True Then Exit Sub

    
' 你可以不需要打开数据库连接对象而直接打开记录集对象,但如果你需要打开多个记录集对象的话,效率是很低的。
    ' 如果你不创建一个数据库连接对象,ADO会在每个记录集打开时自动创建一个新的数据库连接对象,就算你用的是相同的SQL语句。
    Set oConn = Server.CreateObject("ADODB.Connection")

    
On Error Resume Next

    
'Provider=SQLOLEDB.1;Server=(local);Initial Catalog =cx_soft;Integrated Security=SSPI;
    'Provider=SQLOLEDB.1;Server=(local);Initial Catalog =cx_soft;Trusted_Connection=yes;
    oConn.Open "Provider=sqloledb.1;Data Source=(local);Initial Catalog=AspNetTest;User Id=sa;Password=;"
    
    
If Err.Number > 0 Then
        
' 完全地退出正在运行的脚本
        Response.End
    
End If

    
' 创建一个记录集
    
End Sub

Sub DBConnEnd()
    
On Error Resume Next
    oRs.Close
    
Set oRs = Nothing
    oConn.Close
    
Set oConn = Nothing
End Sub




%
>

asp.net代码:

using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;

namespace AspNetTest.Common
{
    
/// <summary>
    
/// tree_DataTable 的摘要说明。
    
/// </summary>

     
    
public class tree_DataTable : System.Web.UI.Page
    
{
        
private DataTable dtPowerTree = new DataTable();
        
const string tablename = "tree";
        
private void Page_Load(object sender, System.EventArgs e)
        
{
            FillTreeDataTable();
            blTreeDataTable(
0);
            dtPowerTree.Clear();            
            
// 在此处放置用户代码以初始化页面
        }

        
private void FillTreeDataTable()
        
{
            
string ConnectionString = System.Configuration.ConfigurationSettings.AppSettings["ConnectionString"];
            SqlConnection conn 
= new SqlConnection(ConnectionString);
            SqlDataAdapter da 
= new SqlDataAdapter("select Id,PowerName,Layer,ParentId from " + tablename, conn);
            da.Fill(dtPowerTree);
        }

        
private void blTreeDataTable(int _ParentId)
        
{
            
string filter = "ParentId=" + _ParentId;
            
string sort = "Id ASC";
            DataRow[] drs 
= dtPowerTree.Select(filter, sort);
            
for(int i=0; i<drs.Length; i++)
            
{
                
if(Convert.ToInt32(drs[i][3]) == _ParentId)
                
{
                    
int Id = Convert.ToInt32(drs[i][0]);
                    
string PowerName = drs[i][1].ToString();
                    
int Layer = Convert.ToInt32(drs[i][2]);
                    
for(int space=1; space<=Layer; space++)
                    
{
                        Page.Response.Write(
"&nbsp;&nbsp;&nbsp;&nbsp;");
                    }

                    Page.Response.Write(PowerName 
+ "<br>");
                    blTreeDataTable(Id);
                }

            }

        }

        
Web 窗体设计器生成的代码
    }

}


遍历结果:



posted @ 2006-04-21 22:25  李振波  阅读(535)  评论(0编辑  收藏  举报