//************************* 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(cmdstr,Con);
try
{
Con.Open();
OleDbDataReader reader = cmd.ExecuteReader();
while(reader.Read())
{
TreeNode childNode = new TreeNode();
childNode.Text = reader["content"].ToString();
childNode.NodeData = reader["id"].ToString();
//***************** 判断是否有子节点 ************ //
// deeptree 为表名
cmdstr = "select count(*) from deeptree where parentid="+childNode.NodeData;
OleDbConnection Con1 = new OleDbConnection(dataConStr);
OleDbCommand cmd1 = new OleDbCommand(cmdstr,Con1);
Con1.Open();
OleDbDataReader reader1 = cmd1.ExecuteReader();
if(reader1.Read())
{
if(System.Convert.ToInt32(reader1[0]) > 0)
{
AddTestNodes(childNode); // 若有子节点,则添加测试节点
}
}
reader1.Close();
Con1.Close();
//************************************************* //
node.Nodes.Add(childNode);
}
}
catch(Exception ex)
{
Label3.Text = ex.Message;
}
finally
{
if(Con.State != ConnectionState.Closed)
Con.Close();
}
}
#region Web 窗体设计器生成的代码
override protected void OnInit(EventArgs e)
{
//
// CODEGEN: 该调用是 ASP.NET Web 窗体设计器所必需的。
//
InitializeComponent();
base.OnInit(e);
}
/// <summary>
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
/// 此方法的内容。
/// </summary>
private void InitializeComponent()
{
this.TreeView1.Expand += new Microsoft.Web.UI.WebControls.ClickEventHandler(this.TreeView1_Expand);
this.Load += new System.EventHandler(this.Page_Load);
}
#endregion
private void TreeView1_Expand(object sender, Microsoft.Web.UI.WebControls.TreeViewClickEventArgs e)
{
Label1.Text = e.Node;
TreeNode node = new TreeNode();
node = TreeView1.GetNodeFromIndex(e.Node);
//node = TreeView1.GetNodeFromIndex("0.2");
if(node != null)
{
Label2.Text = node.NodeData;
// 判断该节点下子节点数,若1个且为testnode说明未填充所以子节点
if((node.Nodes.Count == 1)&& (node.Nodes[0].NodeData == "testnode"))
{
node.Nodes.Clear(); //清空
AddNodes(node); // 填充子节点
}
}
}
}
}
数据库中设置字段为 id, content,parentid
PS: 缺点,因为AutoPostBack为TRUE,提交到服务器时每次都会闪烁. 而且载入数据越多越慢..
没有 用ASP 来的快.
本文地址:http://com.8s8s.com/it/it7073.htm