首页 新闻 会员 周边 捐助

treeview 不显示子节点 只显示父节点

0
悬赏园豆:5 [待解决问题]

protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
           // PopulateTreeView();
            SqlConnection sqlcon = new SqlConnection(ConfigurationManager.AppSettings["con"].ToString());
            string sql = "select purview from wk_role where roleid=10";
            sqlcon.Open();
            SqlCommand cmd = new SqlCommand(sql, sqlcon);
            SqlDataReader reader = cmd.ExecuteReader();
            reader.Read();
            string purview = reader["purview"].ToString();
            reader.Dispose();

            string sqlcom = "select value from wk_menu  ";
            cmd = new SqlCommand(sqlcom, sqlcon);
            SqlDataReader reader1 = cmd.ExecuteReader();
            while (reader1.Read())
            {
                string value1 = reader1["value"].ToString();
                //Response.Write(value1);
                if (purview.Contains(value1))
                {
                    Response.Write(value1);
                    PopulateTreeView(value1);
                }

            }
           
           
            //sqlcon.Close();

 

 


        }
    }
    public void PopulateTreeView(string value1)
    {
        DataTable treeViewData = GetTreeViewData(value1);
        AddTopViewNodes(treeViewData);

 


    }
    private DataTable GetTreeViewData(string value1)
    {
       
        SqlConnection sqlcon = new SqlConnection(ConfigurationManager.AppSettings["con"].ToString());
        string slecteCommand = "select id,parentid,text,value,url,target from wk_menu where value='" + value1 + "' ";

        SqlDataAdapter dad = new SqlDataAdapter(slecteCommand, sqlcon);
        DataTable dt = new DataTable();
        dad.Fill(dt);
        return dt;
        //sqlcon.Close();
    }
    private void AddTopViewNodes(DataTable treeViewData)
    {
        DataView view = new DataView(treeViewData);
        view.RowFilter = "parentid is null";
        foreach (DataRowView row in view)
        {

            TreeNode newnode = new TreeNode(row["text"].ToString(), row["value"].ToString());
            newnode.NavigateUrl = row["url"].ToString().Trim();
            newnode.Target = row["target"].ToString().Trim();

            TreeView1.Nodes.Add(newnode);
            AddChildTreeViewNodes(treeViewData, newnode);


        }

    }
    private void AddChildTreeViewNodes(DataTable treeViewData, TreeNode parentTreeViewNode)
    {
        DataView view = new DataView(treeViewData);

       // Response.Write(parentTreeViewNode.Value);
        view.RowFilter = "parentid = " + parentTreeViewNode.Value;
      
        foreach (DataRowView row in view)
        {
            TreeNode newnode = new TreeNode(row["text"].ToString(), row["value"].ToString());
            newnode.NavigateUrl = row["url"].ToString().Trim();
            newnode.Target = row["target"].ToString().Trim();
            parentTreeViewNode.ChildNodes.Add(newnode);
            AddChildTreeViewNodes(treeViewData, newnode);

        }

    }

IT_Farmer的主页 IT_Farmer | 初学一级 | 园豆:196
提问于:2012-05-29 09:57
< >
分享
所有回答(3)
0

for循环中的  AddChildTreeViewNodes(treeViewData, newnode);这个不要了,只循环一下,就是求最上层的父节点啊。

悟行 | 园豆:12559 (专家六级) | 2012-05-29 10:01

不行呢  你能否加我qq帮我远程一下  2573393999  谢谢了

支持(0) 反对(0) IT_Farmer | 园豆:196 (初学一级) | 2012-05-29 10:08
0

参考下这里 http://www.cnblogs.com/psforever/archive/2012/05/09/2491048.html

psforever | 园豆:461 (菜鸟二级) | 2012-06-01 23:05
0

一樓的就是答案了

   private void AddTopViewNodes(DataTable treeViewData)
    {
        DataView view = new DataView(treeViewData);
        view.RowFilter = "parentid is null";
        foreach (DataRowView row in view)
        {

            TreeNode newnode = new TreeNode(row["text"].ToString(), row["value"].ToString());
            newnode.NavigateUrl = row["url"].ToString().Trim();
            newnode.Target = row["target"].ToString().Trim();

            TreeView1.Nodes.Add(newnode);
            // AddChildTreeViewNodes(treeViewData, newnode); //<- 這個註解掉


        }

    }

yuanlin | 园豆:211 (菜鸟二级) | 2012-06-04 23:14
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册