当前位置:编程学习 > C#/ASP.NET >>

TreeView树形结构的使用

TreeView树形结构怎么把它分3层以上,而且最底层可以像CSDN的左侧一样,可以删除或者是添加某一项???请问有没有这样的实例可以参考。。。实现思路又是怎样的呢??? --------------------编程问答-------------------- http://hi.baidu.com/royler/blog/item/0ec2134fc9658cc6d0c86a34.html

http://www.cnblogs.com/longren629/archive/2007/03/14/674633.html

看看有用不 --------------------编程问答-------------------- 这个没有写过,实例也许都没有,多找点资料吧。 --------------------编程问答-------------------- CSDN --------------------编程问答-------------------- treeNode里面在嵌套treeNode就可以实现3层了  至于添加和删除 就不是很清楚了  --------------------编程问答-------------------- mark --------------------编程问答-------------------- 递归有时还是比较实用的,树型结构我都是自己画的

--------------------编程问答-------------------- 有兴趣 --------------------编程问答-------------------- 学习下 有意思 --------------------编程问答-------------------- Mark了。。。 --------------------编程问答-------------------- http://blog.csdn.net/tautaulee/archive/2008/11/15/3307108.aspx --------------------编程问答-------------------- 实例可以参考 --------------------编程问答-------------------- --------------------编程问答--------------------
引用 6 楼 liuchaolin 的回复:
递归有时还是比较实用的,树型结构我都是自己画的


就是这种效果,不过要有删除/添加的功能,就像CSDN的左侧。。。 --------------------编程问答-------------------- 没有实现过有删除和添加功能的,学习下 --------------------编程问答-------------------- 没有实现过有删除和添加功能的,学习下 --------------------编程问答-------------------- jstree

或者百度 梅花雪树形 --------------------编程问答--------------------
引用 16 楼 wxr0323 的回复:
jstree

或者百度 梅花雪树形


用TreeView实现不了吗???必须要用JS或是Jquery??? --------------------编程问答-------------------- 用ASP.NET中的控件可以实现的。网上很多资料的吧。 --------------------编程问答--------------------
引用 18 楼 myshow0001 的回复:
用ASP.NET中的控件可以实现的。网上很多资料的吧。


继续了。。。 --------------------编程问答--------------------
引用 17 楼 sswp7 的回复:
引用 16 楼 wxr0323 的回复:
jstree

或者百度 梅花雪树形


用TreeView实现不了吗???必须要用JS或是Jquery???

可以 实现 --------------------编程问答--------------------
引用 20 楼 wxr0323 的回复:
引用 17 楼 sswp7 的回复:

引用 16 楼 wxr0323 的回复:
jstree

或者百度 梅花雪树形


用TreeView实现不了吗???必须要用JS或是Jquery???



可以 实现


能说说思路吗??? --------------------编程问答-------------------- 加载节点可以从数据库里面把数据读取出来用递归添加节点,添加先获取你要把当前节点添加到哪个节点下面,修改和删除可以先获取你要操作的节点的编号,之后楼主应该知道怎么操作了吧 --------------------编程问答-------------------- 走一个。。。 --------------------编程问答-------------------- <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="TreeViewDemo._Default" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <asp:TreeView ID="TreeView1" runat="server" Height="257px" ImageSet="WindowsHelp"  
          Width="142px">  
            <ParentNodeStyle Font-Bold="False" />  
            <HoverNodeStyle Font-Underline="True" ForeColor="#6666AA" />  
            <SelectedNodeStyle BackColor="#B5B5B5" Font-Underline="False" HorizontalPadding="0px"  
                VerticalPadding="0px" />  
            <NodeStyle Font-Names="Tahoma" Font-Size="8pt" ForeColor="Black" HorizontalPadding="5px"  
                NodeSpacing="0px" VerticalPadding="1px" />  
        </asp:TreeView>

        
    </div>
    </form>
</body>
</html>

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

using System.Collections;

using web.DAL;  //添加的引用  项目内部的
using web.Model;  //实体

namespace TreeViewDemo
{
    public partial class _Default : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            tree();
        }


        protected void tree()
        {
            TreeNode node = new TreeNode();  //初始化根目录
            node.Text = "根目录";
            List<Treeviews> list = TreeViewManager.getTvById(0);//查询第一节目录
            foreach (Treeviews tv in list)
            {
                TreeNode node1 = new TreeNode();//第一节目录
                node1.Text = tv.Name; //第一节目录名称
                node1.NavigateUrl = tv.UrlPath;  //第一节目录路径
                node.ChildNodes.Add(node1);  //添加到根目录节点

                List<Treeviews> list2 = TreeViewManager.getTvById(tv.Id);  //查询第二节目录
                foreach (Treeviews tv2 in list2)
                {
                    TreeNode node2 = new TreeNode();   //第二节点
                    node2.Text = tv2.Name;   //第二节点名称
                    node2.NavigateUrl = tv2.UrlPath;  //第二节点路径
                    node1.ChildNodes.Add(node2);  //添加到第一节点

                    List<Treeviews> list3 = TreeViewManager.getTvById(tv2.Id);
                    foreach (Treeviews tv3 in list3)
                    {
                        TreeNode node3 = new TreeNode();    //第三节节点
                        node3.Text = tv3.Name;   //第三节节点名称
                        node3.NavigateUrl = tv3.UrlPath;   //第三节点路径
                        node2.ChildNodes.Add(node3);
                    }
                }
            }

            TreeView1.Nodes.Add(node);   //添加根目录到treeview控件
        }
    }
}

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.SqlClient;
using System.Data;
using System.Data.Sql;
using web.Model;

namespace web.DAL
{
    public class TreeViewManager
    {
        public static List<Treeviews> getTvById(int id)
        {
            string sql = "select * from Treeviews where pid=@id order by Id asc";
            SqlParameter[] values = { new SqlParameter("@id",id)};
            List<Treeviews> list = getDataTable(sql, values);
            return list;
            
        }


        private static List<Treeviews> getDataTable(string sql, SqlParameter[] values)
        {
            using (DataTable dt = DBHelper.GetDataTable(sql, values))
            {
                List<Treeviews> list = new List<Treeviews>();
                foreach (DataRow row in dt.Rows)
                {
                    Treeviews tv = new Treeviews();
                    tv.Id=(int)row["Id"];
                    tv.Name = row["Name"].ToString();
                    tv.PId = (int)row["PId"];
                    tv.UrlPath = row["UrlPath"].ToString();
                    list.Add(tv);
                }
                return list;
            }
        }



    }
}
--------------------编程问答-------------------- zree也很好 --------------------编程问答-------------------- 打错了,是ztree --------------------编程问答--------------------
引用 26 楼 wowtejinjie 的回复:
打错了,是ztree


不错嘛??? --------------------编程问答-------------------- 给你写个demo吧。不过恐怕你难以看懂。没有办法,现在asp.net程序员是.net程序员中水平最低的一类人员,他们往往不去学asp.net机制,动不动就用“asp.net做不了”来回答问题。如果没有真正学过asp.net,很可能连 asp.net 技术中最基本的 IPostBackEventHandler 都不知道为何物,那么要看懂这个代码可能有点难度。

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="TestLoadTreeView.aspx.cs" Inherits="_TestLoadTreeView" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title>演示装载TreeView控件,并为TreeNode添加两个功能链接</title>
</head>
<body>
    <form id="form1" runat="server">
    <asp:Button ID="Button1" runat="server" Text="回发测试" onclick="Button1_Click" /> <asp:Button ID="Button2"
        runat="server" Text="创建树" onclick="Button2_Click" />
        <hr />
    <asp:PlaceHolder ID="PlaceHolder1" runat="server"></asp:PlaceHolder>
    </form>
</body>
</html>
using System;
using System.Drawing;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class _TestLoadTreeView : System.Web.UI.Page, IPostBackEventHandler
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (ViewState["treeview"] != null && (bool)ViewState["treeview"])
            PlaceHolder1.Controls.Add(new TreeView { ID = "myTestTree" });
    }
    protected void Button1_Click(object sender, EventArgs e)
    {

    }

    //重新设置演示数据
    protected void Button2_Click(object sender, EventArgs e)
    {
        PlaceHolder1.Controls.Clear();
        var tr = new TreeView { ID = "myTestTree" };
        PlaceHolder1.Controls.Add(tr);
        tr.ShowLines = true;
        tr.SelectedNodeStyle.BackColor = Color.LightCyan;
        var n = new TreeNode { Text = "n1" };
        tr.Nodes.Add(n);
        n = new TreeNode { Text = "n2" };
        tr.Nodes.Add(n);
        var m = new TreeNode { Text = "n3" };
        n.ChildNodes.Add(m);
        ViewState["treeview"] = true;
        为所有节点添加两个功能(tr);
    }

    private void 为所有节点添加两个功能(TreeView tr)
    {
        为所有节点添加两个功能(tr.Nodes);
    }

    private void 为所有节点添加两个功能(TreeNodeCollection treeNodeCollection)
    {
        foreach (TreeNode n in treeNodeCollection)
        {
            n.Text += "  <a href=\"javascript:" + this.ClientScript.GetPostBackEventReference(this, "func1_" + n.Text) +
                "\">功能1</a> <a href=\"javascript:" + this.ClientScript.GetPostBackEventReference(this, "func2_" + n.Text) +
                "\">功能2</a>";
            为所有节点添加两个功能(n.ChildNodes);
        }
    }


    public void RaisePostBackEvent(string eventArgument)
    {
        string s;
        if (eventArgument.StartsWith("func1_"))
            s = "第一项功能: " + eventArgument.Substring(6);
        else if (eventArgument.StartsWith("func2_"))
            s = "第二项功能: " + eventArgument.Substring(6);
        else
            return;

        ScriptManager.RegisterStartupScript(this, this.GetType(), "功能", "alert('" + s + "');", true);
    }
}
补充:.NET技术 ,  ASP.NET
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,