日期:2012-08-29  浏览次数:20918 次

 

//*************************  aspx  *************************//

<%@ Page language="c#" Codebehind="WebForm1.aspx.cs" AutoEventWireup="false" Inherits="TreeTest.WebForm1" %>
<%@ Register TagPrefix="iewc" Namespace="Microsoft.Web.UI.WebControls" Assembly="Microsoft.Web.UI.WebControls" %>

<HTML>
 <body MS_POSITIONING="GridLayout">
  <form id="Form1" method="post" runat="server">
   <FONT face="宋体">
    <iewc:TreeView id="TreeView1" style="Z-INDEX: 101; LEFT: 8px; POSITION: absolute; TOP: 16px" runat="server"
     AutoPostBack="True"></iewc:TreeView>
    <asp:Label id="Label1" style="Z-INDEX: 102; LEFT: 440px; POSITION: absolute; TOP: 56px" runat="server"
     Width="152px" Height="64px">Label</asp:Label>
    <asp:Label id="Label2" style="Z-INDEX: 103; LEFT: 456px; POSITION: absolute; TOP: 144px" runat="server"
     Width="144px" Height="48px">Label</asp:Label>
    <asp:Label id="Label3" style="Z-INDEX: 104; LEFT: 696px; POSITION: absolute; TOP: 64px" runat="server">Label</asp:Label></FONT>
  </form>
 </body>
</HTML>

//*****************************  CS *************************//

using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using System.Data.OleDb;
using Microsoft.Web.UI.WebControls;
namespace TreeTest
{
 /// <summary>
 /// WebForm1 的摘要说明。
 /// </summary>
 public class WebForm1 : System.Web.UI.Page
 {
  protected Microsoft.Web.UI.WebControls.TreeView TreeView1;
  protected System.Web.UI.WebControls.Label Label1;
  protected System.Web.UI.WebControls.Label Label2;
  protected System.Web.UI.WebControls.Label Label3;
  
  OleDbConnection Con ;
  string dataConStr = "";

  private void Page_Load(object sender, System.EventArgs e)
  {
   dataConStr = "Provider=microsoft.jet.oledb.4.0;Data Source="+Server.MapPath("tree.mdb")+";User ID=;Password=;";
   
   Con = new OleDbConnection(dataConStr);
   if(!IsPostBack)
   {
                   
    TreeNode node = new TreeNode();
    node.Text = "根";
    node.NodeData = "0";
    TreeView1.Nodes.Add(node); // 添加根
    AddTestNodes(node); //添加"展开..."节点
   }
  }

  private void AddTestNodes(TreeNode node)
  {
   TreeNode testnode = new TreeNode();
   testnode.Text = "正在展开....";
   testnode.NodeData = "testnode";
   node.Nodes.Add(testnode);
  }
  private void AddNodes(TreeNode node)
  {
   string parentID = "0";
   if(node.Parent != null)
   {
    parentID = node.NodeData;
   }

  // deeptree 为表名
   string cmdstr = "select * from deeptree where parentid="+parentID;

   OleDbCommand cmd = new OleDbCommand(cmd