webform 创建树

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;

namespace WebApplication4
{
    public partial class WebForm1 : System.Web.UI.Page
    {
        DataTable _dt = new DataTable();
        protected void Page_Load(object sender, EventArgs e)
        {
            _dt.Columns.Add("code");
            _dt.Columns.Add("parentCode");
            _dt.Columns.Add("name");

            DataRow dr=_dt.NewRow();
            dr[0]="01";
            dr[1]="0";
            dr[2]="顶层1";
            _dt.Rows.Add(dr);

            dr = _dt.NewRow();
            dr[0] = "011";
            dr[1] = "01";
            dr[2] = "子层1";
            _dt.Rows.Add(dr);

            dr = _dt.NewRow();
            dr[0] = "012";
            dr[1] = "01";
            dr[2] = "子层2";
            _dt.Rows.Add(dr);


            dr = _dt.NewRow();
            dr[0] = "02";
            dr[1] = "0";
            dr[2] = "顶层2";
            _dt.Rows.Add(dr);

            dr = _dt.NewRow();
            dr[0] = "021";
            dr[1] = "02";
            dr[2] = "子层1";
            _dt.Rows.Add(dr);

            dr = _dt.NewRow();
            dr[0] = "022";
            dr[1] = "02";
            dr[2] = "子层2";
            _dt.Rows.Add(dr);

            dr = _dt.NewRow();
            dr[0] = "0221";
            dr[1] = "022";
            dr[2] = "子子层1";
            _dt.Rows.Add(dr);

            BuildTree(null, _dt);
        }


        /// <summary>
        /// 绑定树
        /// </summary>
        /// <param name="tn"></param>
        private void BuildTree(TreeNode tn, DataTable dtDept)
        {
            int i;

            // 空节点时创建根节点,父ID为0的当作根节点
            if (tn == null)
            {
                this.TreeView1.Nodes.Clear();
                for (i = 0; i < dtDept.Rows.Count; i++)
                {
                    if (dtDept.Rows[i]["parentCode"].ToString() == "0")
                    {
                        TreeNode tmp = new TreeNode(dtDept.Rows[i]["name"].ToString());
                        tmp.Value = dtDept.Rows[i]["code"].ToString();
                        tmp.ToolTip = dtDept.Rows[i]["parentCode"].ToString();
                        tmp.Checked = true;
                        TreeView1.Nodes.Add(tmp);
                    }
                }
                // 循环递归创建树
                for (i = 0; i < TreeView1.Nodes.Count; i++)
                {
                    BuildTree(TreeView1.Nodes[i],dtDept);
                }
            }
            else // 节点非空为递归调用
            {
                for (i = 0; i < dtDept.Rows.Count; i++)
                {
                    if (tn.Value.ToString() == dtDept.Rows[i]["parentCode"].ToString())
                    {
                        TreeNode tmp = new TreeNode(dtDept.Rows[i]["name"].ToString());
                        tmp.Value = dtDept.Rows[i]["code"].ToString();
                        tmp.ToolTip = dtDept.Rows[i]["parentCode"].ToString();          
                        tn.ChildNodes.Add(tmp);
                    }
                }
                for (i = 0; i < tn.ChildNodes.Count; i++)
                {
                    BuildTree(tn.ChildNodes[i],dtDept);
                }
            }
        }

        protected void TreeView1_SelectedNodeChanged(object sender, EventArgs e)
        {
            this.Label1.Text = TreeView1.SelectedValue;
         
            ScriptManager.RegisterStartupScript(UpdatePanel1, this.GetType(), "updatePanel1show", "change_css(‘" + Label1.Text + "‘)", true);
            TreeView1.ExpandAll();

        }
    }
}

郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。