Tree:

namespace ECTECH.NorthSJ.Web.SysData
{
public partial class testTree : BasePage
{
protected int roleId = ; protected void Page_Load(object sender, EventArgs e)
{
if(!IsPostBack)
{
List<Model.Funcs> list = new BLL.RoleFuncs().GetRFunc("", roleId);
foreach(Model.Funcs func in list)
{
FineUI.TreeNode node = new TreeNode() { Text = func.Name, NodeID = func.ID.ToString(), Leaf = false, EnableCheckBox=true };
LoadChildNode(func.FuncCode,roleId, node);
tree1.Nodes.Add(node);
}
}
} private void LoadChildNode(string parentCode, int roleId,TreeNode node)
{
List<Model.Funcs> list = new BLL.RoleFuncs().GetRFunc(parentCode, roleId);
foreach (Model.Funcs func in list)
{
TreeNode nodeChild = new TreeNode() { Text = func.Name, NodeID = func.ID.ToString(), Leaf = true, EnableCheckBox = true };
node.Nodes.Add(nodeChild);
}
} protected void btn1_Click(object sender, EventArgs e)
{
string[] ids = tree1.SelectedNodeIDArray;
string idStr = string.Join(",", ids);
lbResult.Text = idStr;
} }

Form:

    <form id="form1" runat="server">
<div>
<x:PageManager ID="PageManager1" AutoSizePanelID="RegionPanel1" runat="server">
</x:PageManager>
<x:Tree ID="tree1" EnableMultiSelect="true" runat="server"></x:Tree>
<x:Button ID="btn1" Text="确定" runat="server" OnClick="btn1_Click"></x:Button>
<x:Label ID="lbResult" EncodeText="false" runat="server"></x:Label>
</div>
</form>

树节点,绑定到DataTable:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI; using System.Data;
using FineUI; namespace Test_Web1
{
public partial class TreeTable : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if(! IsPostBack)
{
LoadData();
}
} private void LoadData()
{
DataTable dt = CreateTable();
DataSet ds = new DataSet();
ds.Tables.Add(dt);
ds.Relations.Add("treeRelation", ds.Tables[].Columns["ID"],ds.Tables[].Columns["PID"]); foreach(DataRow row in ds.Tables[].Rows)
{
if(row["PID"]==DBNull.Value)
{
TreeNode node = new TreeNode();
node.Text = row["Name"].ToString();
node.NodeID = row["ID"].ToString();
node.Expanded = true;
Tree1.Nodes.Add(node); ResolveSubTree(row, node);
}
}
} private void ResolveSubTree(DataRow row, TreeNode node)
{
DataRow[] rows = row.GetChildRows("treeRelation");
if(rows.Length>)
{
foreach(DataRow r in rows)
{
TreeNode n = new TreeNode();
n.Text = r["Name"].ToString();
n.NodeID = r["ID"].ToString();
n.ParentNode = node;
node.Nodes.Add(n); ResolveSubTree(r, n);
}
}
} public DataTable CreateTable()
{
DataTable dt = new DataTable();
dt.Columns.Add("ID", typeof(string));
dt.Columns.Add("Name", typeof(string));
dt.Columns.Add("PID", typeof(string)); DataRow row = dt.NewRow();
row["ID"] = "";
row["Name"] = "四川省";
row["PID"] = DBNull.Value;
dt.Rows.Add(row); row = dt.NewRow();
row["ID"] = "";
row["Name"] = "江苏省";
row["PID"] = DBNull.Value;
dt.Rows.Add(row); row = dt.NewRow();
row["ID"] = "";
row["Name"] = "苏州市";
row["PID"] = "";
dt.Rows.Add(row); row = dt.NewRow();
row["ID"] = "";
row["Name"] = "常州市";
row["PID"] = "";
dt.Rows.Add(row); row = dt.NewRow();
row["ID"] = "";
row["Name"] = "镇江市";
row["PID"] = "";
dt.Rows.Add(row); row = dt.NewRow();
row["ID"] = "";
row["Name"] = "无锡市";
row["PID"] = "";
dt.Rows.Add(row); row = dt.NewRow();
row["ID"] = "";
row["Name"] = "达州市";
row["PID"] = "";
dt.Rows.Add(row); row = dt.NewRow();
row["ID"] = "";
row["Name"] = "广元市";
row["PID"] = "";
dt.Rows.Add(row); row = dt.NewRow();
row["ID"] = "";
row["Name"] = "宣汉县";
row["PID"] = "";
dt.Rows.Add(row); row = dt.NewRow();
row["ID"] = "";
row["Name"] = "大竹县";
row["PID"] = "";
dt.Rows.Add(row); return dt; }
}
}
04-25 00:15