C# 动态创建树视图

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/9096664/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-09 05:54:53  来源:igfitidea点击:

Dynamically create a treeview

c#asp.netarchitecturetreeviewlazy-loading

提问by user929153

I am trying to create a treeview dynamically using c# and asp.net.

我正在尝试使用 c# 和 asp.net 动态创建树视图。

I have created a lazy load treeview using the populate ondemand attribute.

我使用 populate ondemand 属性创建了一个延迟加载树视图。

>  <asp:TreeView ID="treeView1"  runat="server" 
>              OnTreeNodePopulate="treeview1_TreeNodePopulate"></asp:TreeView>

Behind code I have loaded my data but initially I populate the parent nodes. What I want to achieve is when i click on parent node I then do a postback and then populate its child and then again populate its child's and so now. I have thousands of data so i dont want all data to be populated due to performance. So thats the reason why I only want to populate the node childs based on selected node. See example below:

在代码后面我已经加载了我的数据,但最初我填充了父节点。我想要实现的是,当我单击父节点时,我进行回发,然后填充其子节点,然后再次填充其子节点,等等。我有成千上万的数据,所以我不希望由于性能而填充所有数据。所以这就是为什么我只想根据所选节点填充节点子节点的原因。请参阅下面的示例:

>Peter
    - - >user1
    - - >user2
    - - >user3
       - - >userPassword
       - - >userId
>john
>david
>Hyman
    - - >user1
    - - >user2
       - - >userpassword
       - - >userId
       - - >Permissions
>Laura 
    - - > admin
    - - > permissions
       -- > user1
       -- > user2
         - - >userpassword
             - - >userId
             - - >Permissions           
>...
>...
>...

As you can see there can be multiple parent nodes and multiple layers. These will be populated dynically based on what i pass in to DB. Everytime i click on node it will expand the node and populate its child using postback and then when you click on its child again it will do a postback and populate its child again etc. So i wanted help on how to create a dynamic treeview.

如您所见,可以有多个父节点和多个层。这些将根据我传递给 DB 的内容动态填充。每次我单击节点时,它都会扩展节点并使用回发填充其子节点,然后当您再次单击其子节点时,它将执行回发并再次填充其子节点等。所以我需要有关如何创建动态树视图的帮助。

c# :

C# :

private void LoadTreeview()
{
 //Load data
 // Get data from DB.
 //loop through the list and build its parent nodes.
  foreach (var dxm in list)
  {
                TreeNode tnParent = CheckNodeExist(dxm.Node); //I check to see if exists.
                if (tnParent== null)
                {
                    TreeNode tn = new TreeNode();
                    tn.Text = dxm.Node;
                    tn.Value = dxm.Id.ToString();
                    tn.SelectAction = TreeNodeSelectAction.None;
                    tn.Collapse();
                    treeView1.Nodes.Add(tn);
                    tn.PopulateOnDemand = true; //lazy load
                    tnParent= tn;
                }

}

This method above is called on page load.

上面的这个方法在页面加载时调用。

On TreeNodePopulateEvent: (when a node is clicked on)

在 TreeNodePopulateEvent 上:(当一个节点被点击时)

protected void treeview1_TreeNodePopulate(object sender, TreeNodeEventArgs e)
        {
            ICollection<ITEMS> list = new Collection<ITEMS>();           

            list = GetData(e.Node.Text); //pass in the node you have selected  this will go and check in DB if the node does have any child nodes. If so will return with child nodes.

            foreach (var dxm in list)
            {

                TreeNode tnChild = CheckNodeExist(dxm.Node);
                if (tnChild == null)
                {
                    TreeNode tn = new TreeNode();
                    tn.Text = dxm.Node;
                    tn.Value = dxm.Id.ToString();
                    tn.SelectAction = TreeNodeSelectAction.None;
                    tn.Collapse();

                    tn.PopulateOnDemand = true;
                    tnChild = tn;
                    tnChild.ChildNodes.Add(tnChild);                  

                }
            }
        }

采纳答案by Jeff Turner

I believe you are looking for the SelectedNodeChanged event. You should be able to load your child nodes in this event. Basically this event will be fired everytime you select a node by clicking on it.

我相信您正在寻找 SelectedNodeChanged 事件。您应该能够在此事件中加载您的子节点。基本上,每次您通过单击选择一个节点时,都会触发此事件。

Your aspx

你的aspx

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_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 id="div1" runat="server">

    </div>

    <asp:TreeView ID="TreeView1" runat="server" 
        onselectednodechanged="TreeView1_SelectedNodeChanged">

    </asp:TreeView>

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

Your codebehind

你的代码隐藏

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

public partial class _Default : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {
            TreeView1.Nodes.Add(new TreeNode("Node1"));
            TreeView1.Nodes[0].ChildNodes.Add(new TreeNode("ChildNode"));
        }

    }


    protected void TreeView1_SelectedNodeChanged(object sender, EventArgs e)
    {
        Response.Write(TreeView1.SelectedNode.Text);
    }
}