aspx:
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="testtree2.aspx.cs" Inherits="testtree2" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head id="Head1" runat="server">
<title>Untitled Page</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:TreeView ID="TreeView1" runat="server" ExpandDepth="0" ImageSet="XPFileExplorer" NodeIndent="15" EnableClientScript="false" OnTreeNodePopulate="PopulateNode">
<ParentNodeStyle Font-Bold="False" />
<SelectedNodeStyle BackColor="#B5B5B5" Font-Underline="False" HorizontalPadding="0px"
VerticalPadding="0px" />
<NodeStyle Font-Names="Tahoma" Font-Size="8pt" ForeColor="Black" HorizontalPadding="2px"
NodeSpacing="0px" VerticalPadding="2px" />
<HoverNodeStyle Font-Underline="True" ForeColor="#6666AA" />
</asp:TreeView>
</div>
</form>
</body>
</html>
cs:
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.OracleClient;
// ODP.NET Import(s)
using Oracle.DataAccess.Client;
using Oracle.DataAccess.Types;
public partial class testtree2 : System.Web.UI.Page
{
//private const string dbConnString = "Data Source=testdb;User ID=myerp; Password=myerp;";
OracleDataAccess oda = new OracleDataAccess("TestConnectionString");
public string pid = "1";
//private const string cataQuery = "SELECT * FROM DAT_CATA";
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
TreeView1.Nodes.Clear();
AddTree(Int32.Parse(pid), (TreeNode)null);
}
}
protected void PopulateNode(Object sender, TreeNodeEventArgs e)
{
// 判斷節點深度
switch (e.Node.Depth)
{
case 0:
// 產生第一層節點
AddTree(Int32.Parse(pid), e.Node);
break;
case 1:
//產生第二層節點
AddTree(Int32.Parse(pid), e.Node);
break;
default:
break;
}
}
public void AddTree(int ParentID, TreeNode pNode)
{
string sql = "SELECT CATA_ID, CATA_NAME, CATA_PID, CATA_NO FROM BASE_CATA";
DataSet ds = oda.getDataSet(sql);
DataView dvTree = new DataView(ds.Tables[0]);
if (pNode == null)
{
dvTree.RowFilter = "[CATA_PID] = 1";
}
else
{
dvTree.RowFilter = "[CATA_PID] = " + pNode.Value;
}
foreach (DataRowView Row in dvTree)
{
TreeNode Node = new TreeNode();
Node.PopulateOnDemand = true;
Node.SelectAction = TreeNodeSelectAction.Expand;
if (pNode == null)
{
Node.Text = Row["CATA_NAME"].ToString();
Node.Value = Row["CATA_ID"].ToString();
TreeView1.Nodes.Add(Node);
Node.Expanded = false;
}
else
{
Node.Text = Row["CATA_NAME"].ToString();
Node.Value = Row["CATA_ID"].ToString();
pNode.ChildNodes.Add(Node);
Node.Expanded = false;
}
}
}
}