码迷,mamicode.com
首页 > Web开发 > 详细

webform 创建树

时间:2014-12-15 18:37:08      阅读:301      评论:0      收藏:0      [点我收藏+]

标签:des   io   ar   os   sp   for   on   art   bs   

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();

        }
    }
}

webform 创建树

标签:des   io   ar   os   sp   for   on   art   bs   

原文地址:http://www.cnblogs.com/movemoon/p/4165185.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!