asp.net 之 数据库导入treeview

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

public partial class Default3 : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        string sqlstr = "select item_id,item_name,parent_id from para_item";
       


        
    }

    protected void addtree(int Pid, TreeNode Pnode) 
    {
        string sqlstr = "select item_id,item_name,parent_id from para_item";
        DataTable dt =SqlHelper.GetTableText(sqlstr, null)[0];
       
        if (dt.Rows.Count > 0)
        {
            DataView dv = new DataView(dt);
            dv.RowFilter = "[Parent_ID]=" + Pid;
            foreach (DataRowView Row in dv)
            {
                //声明节点
                TreeNode node = new TreeNode();
                //绑定超级连接
                node.NavigateUrl = string.Format("javascript:show('{0}')", Row["item_name"].ToString());

                if (Pnode == null)
                {
                    node.Text = Row["item_name"].ToString();
                    TreeView1.Nodes.Add(node);
                    node.Expanded = true; //节点状态
                    addtree(Int32.Parse(Row["Parent_ID"].ToString()), node);


                }
                else {
                    node.Text = Row["item_name"].ToString();
                    Pnode.ChildNodes.Add(node);
                    node.Expanded = true; //节点状态
                    addtree(Int32.Parse(Row["Parent_ID"].ToString()), node);

                
                
                }

            }
        }
    }
}

  

posted @ 2016-10-24 16:15  尘梦  阅读(290)  评论(0编辑  收藏  举报