WebForm中创建树节点TreeNode

Tree:

 

namespace ECTECH.NorthSJ.Web.SysData
{
    public partial class testTree : BasePage
    {
        protected int roleId = 5;

        protected void Page_Load(object sender, EventArgs e)
        {
            if(!IsPostBack)
            {
            List<Model.Funcs> list = new BLL.RoleFuncs().GetRFunc("0", 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[0].Columns["ID"],ds.Tables[0].Columns["PID"]);

            foreach(DataRow row in ds.Tables[0].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>0)
            {
                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"] = "1";
            row["Name"] = "四川省";
            row["PID"] = DBNull.Value;
            dt.Rows.Add(row);

            row = dt.NewRow();
            row["ID"] = "2";
            row["Name"] = "江苏省";
            row["PID"] = DBNull.Value;
            dt.Rows.Add(row);

            row = dt.NewRow();
            row["ID"] = "3";
            row["Name"] = "苏州市";
            row["PID"] = "2";
            dt.Rows.Add(row);

            row = dt.NewRow();
            row["ID"] = "4";
            row["Name"] = "常州市";
            row["PID"] = "2";
            dt.Rows.Add(row);

            row = dt.NewRow();
            row["ID"] = "5";
            row["Name"] = "镇江市";
            row["PID"] = "2";
            dt.Rows.Add(row);

            row = dt.NewRow();
            row["ID"] = "6";
            row["Name"] = "无锡市";
            row["PID"] = "2";
            dt.Rows.Add(row);

            row = dt.NewRow();
            row["ID"] = "7";
            row["Name"] = "达州市";
            row["PID"] = "1";
            dt.Rows.Add(row);

            row = dt.NewRow();
            row["ID"] = "8";
            row["Name"] = "广元市";
            row["PID"] = "1";
            dt.Rows.Add(row);

            row = dt.NewRow();
            row["ID"] = "9";
            row["Name"] = "宣汉县";
            row["PID"] = "7";
            dt.Rows.Add(row);

            row = dt.NewRow();
            row["ID"] = "10";
            row["Name"] = "大竹县";
            row["PID"] = "7";
            dt.Rows.Add(row);

            return dt;

        }
    }
}

 

你可能感兴趣的:(WebForm中创建树节点TreeNode)