我正在使用集合来填充 ASP.NET 中的树视图。我将如何从树中删除节点

发布于 2024-11-02 16:29:21 字数 5746 浏览 0 评论 0原文

using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;

using ETG.TechnicalEditorialDEL;
using ETG.TechnicalEditorialBAL;
using System.Collections.Generic;

namespace TechnicalEditorialWebUI.MasterEditorialPage
{
    public partial class ConfigurationSettings : System.Web.UI.Page
    {
        List<TreeList> lstTreeList = new List<TreeList>();
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                PopulateComponentType();
                PopulateComponent();
            }
        }
    public void PopulateComponentType()
    {
        GetLookUpData getLookUpData = new GetLookUpData();
        ddlComponentType.DataSource = getLookUpData.GetComponentTypes();
        ddlComponentType.DataValueField = "ComponentTypeId";
        ddlComponentType.DataTextField = "ComponentTypeDesc";
        ddlComponentType.DataBind();
    }

    public void PopulateComponent()
    {
        ComponentType componentType = new ComponentType { ComponentTypeId = Convert.ToInt32(ddlComponentType.SelectedValue) };
        GetLookUpData getLookUpData = new GetLookUpData();
        lstComponent.DataSource = getLookUpData.GetComponentByComponentType(componentType);
        lstComponent.DataValueField = "ComponentId";
        lstComponent.DataTextField = "ComponentTypeDesc";
        lstComponent.DataBind();
    }

    protected void ddlComponentType_SelectedIndexChanged(object sender, EventArgs e)
    {
        lstComponent.Items.Clear();
        PopulateComponent();
    }

    protected void btnAdd_Click(object sender, EventArgs e)
    {
        if (ViewState["TreeList"] == null)
        {
            CreateTree(lstTreeList);
        }
        else
        {
            List<TreeList> newLstTree = (List<TreeList>)ViewState["TreeList"];
            CreateTree(newLstTree);
        }
    }

    protected void btnRemove_Click(object sender, EventArgs e)
    {
        //if (ViewState["TreeList"] == null)
        //{

        //    CreateTree(lstTreeList);
        //}
        //else
        {
            RemoveNodeFromTree();
        }
    }

    private void CreateTreeWithNodes()
    {           
        //var newLstTree = ((List<TreeList>)ViewState["TreeList"]).OrderBy(x=>x.Parent).Select(x=>x);

        //var newLstTree = (from t in (List<TreeList>)ViewState["TreeList"]
        //                  orderby t.Parent.Keys ascending
        //                  select t).ToList();



        TreeView1.Nodes.Clear();


        foreach (TreeList item in (List<TreeList>)ViewState["TreeList"])
        {
            TreeNode treeNode = new TreeNode();

            foreach (var key in item.Parent)
            {                    
                treeNode.Text = key.Value;
                treeNode.Value = key.Key;
            }

            foreach (var child in item.Child)
            {
                foreach (var key in child)
                {
                    treeNode.ChildNodes.Add(new TreeNode(key.Value, key.Key));
                }
            }
            TreeView1.Nodes.Add(treeNode);
        }
    }

    private void CreateTree(List<TreeList> lstTreeList)
    {
        TreeList objTreeList = new TreeList();
        objTreeList.Child = new List<Dictionary<string, string>>();
        objTreeList.Parent = new Dictionary<string, string>(); 
        objTreeList.Parent.Add(ddlComponentType.SelectedValue, ddlComponentType.SelectedItem.Text);

        Dictionary<string, string> temp = null;

        foreach (ListItem item in lstComponent.Items)
        {
            if (item.Selected)
            {
                temp = new Dictionary<string, string>();
                temp.Add(item.Value, item.Text);
                objTreeList.Child.Add(temp);

                //  treeNode.ChildNodes.Add(new TreeNode(item.Text, item.Value));
            }
        }

        lstTreeList.Add(objTreeList);

        ViewState["TreeList"] = lstTreeList;
        CreateTreeWithNodes();
    }

    private void RemoveNodeFromTree()
    {
        List<TreeList> newLstTree = lstTreeList;
        string strNode= TreeView1.SelectedNode.Parent.Value;

        TreeList objTreeList = new TreeList();

        //treeNode.Text = TreeView1.SelectedNode.Parent.Text;
        //treeNode.Value = TreeView1.SelectedNode.Parent.Value;          

        //foreach (var  treeListObject in treeView)
        //{
        //    if (treeListObject.Child == TreeView1.SelectedNode.Text)
        //    {

        //    }
        //}

        //treeNode.ChildNodes.Remove(new TreeNode(TreeView1.SelectedNode.Value,  TreeView1.SelectedNode.Text));


        //TreeView1.Nodes.Add(treeNode);

        //Dictionary<string, string> temp = new Dictionary<string,string>();

        //objTreeList.Parent.Add(TreeView1.SelectedNode.Parent.Value, TreeView1.SelectedNode.Parent.Text);

        //temp.Add(TreeView1.SelectedNode.Value,  TreeView1.SelectedNode.Text);
        //objTreeList.Child.Add(temp);

        //newLstTree.Remove(objTreeList);           
    }
}

[Serializable]
class TreeList : IComparable
{
    public Dictionary<string, string> Parent { get; set; }

    public List<Dictionary<string, string>> Child { get; set; }

    #region IComparable Members

    public int CompareTo(object obj)
    {
        throw new NotImplementedException();
    }

    #endregion        
}
 }
using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;

using ETG.TechnicalEditorialDEL;
using ETG.TechnicalEditorialBAL;
using System.Collections.Generic;

namespace TechnicalEditorialWebUI.MasterEditorialPage
{
    public partial class ConfigurationSettings : System.Web.UI.Page
    {
        List<TreeList> lstTreeList = new List<TreeList>();
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                PopulateComponentType();
                PopulateComponent();
            }
        }
    public void PopulateComponentType()
    {
        GetLookUpData getLookUpData = new GetLookUpData();
        ddlComponentType.DataSource = getLookUpData.GetComponentTypes();
        ddlComponentType.DataValueField = "ComponentTypeId";
        ddlComponentType.DataTextField = "ComponentTypeDesc";
        ddlComponentType.DataBind();
    }

    public void PopulateComponent()
    {
        ComponentType componentType = new ComponentType { ComponentTypeId = Convert.ToInt32(ddlComponentType.SelectedValue) };
        GetLookUpData getLookUpData = new GetLookUpData();
        lstComponent.DataSource = getLookUpData.GetComponentByComponentType(componentType);
        lstComponent.DataValueField = "ComponentId";
        lstComponent.DataTextField = "ComponentTypeDesc";
        lstComponent.DataBind();
    }

    protected void ddlComponentType_SelectedIndexChanged(object sender, EventArgs e)
    {
        lstComponent.Items.Clear();
        PopulateComponent();
    }

    protected void btnAdd_Click(object sender, EventArgs e)
    {
        if (ViewState["TreeList"] == null)
        {
            CreateTree(lstTreeList);
        }
        else
        {
            List<TreeList> newLstTree = (List<TreeList>)ViewState["TreeList"];
            CreateTree(newLstTree);
        }
    }

    protected void btnRemove_Click(object sender, EventArgs e)
    {
        //if (ViewState["TreeList"] == null)
        //{

        //    CreateTree(lstTreeList);
        //}
        //else
        {
            RemoveNodeFromTree();
        }
    }

    private void CreateTreeWithNodes()
    {           
        //var newLstTree = ((List<TreeList>)ViewState["TreeList"]).OrderBy(x=>x.Parent).Select(x=>x);

        //var newLstTree = (from t in (List<TreeList>)ViewState["TreeList"]
        //                  orderby t.Parent.Keys ascending
        //                  select t).ToList();



        TreeView1.Nodes.Clear();


        foreach (TreeList item in (List<TreeList>)ViewState["TreeList"])
        {
            TreeNode treeNode = new TreeNode();

            foreach (var key in item.Parent)
            {                    
                treeNode.Text = key.Value;
                treeNode.Value = key.Key;
            }

            foreach (var child in item.Child)
            {
                foreach (var key in child)
                {
                    treeNode.ChildNodes.Add(new TreeNode(key.Value, key.Key));
                }
            }
            TreeView1.Nodes.Add(treeNode);
        }
    }

    private void CreateTree(List<TreeList> lstTreeList)
    {
        TreeList objTreeList = new TreeList();
        objTreeList.Child = new List<Dictionary<string, string>>();
        objTreeList.Parent = new Dictionary<string, string>(); 
        objTreeList.Parent.Add(ddlComponentType.SelectedValue, ddlComponentType.SelectedItem.Text);

        Dictionary<string, string> temp = null;

        foreach (ListItem item in lstComponent.Items)
        {
            if (item.Selected)
            {
                temp = new Dictionary<string, string>();
                temp.Add(item.Value, item.Text);
                objTreeList.Child.Add(temp);

                //  treeNode.ChildNodes.Add(new TreeNode(item.Text, item.Value));
            }
        }

        lstTreeList.Add(objTreeList);

        ViewState["TreeList"] = lstTreeList;
        CreateTreeWithNodes();
    }

    private void RemoveNodeFromTree()
    {
        List<TreeList> newLstTree = lstTreeList;
        string strNode= TreeView1.SelectedNode.Parent.Value;

        TreeList objTreeList = new TreeList();

        //treeNode.Text = TreeView1.SelectedNode.Parent.Text;
        //treeNode.Value = TreeView1.SelectedNode.Parent.Value;          

        //foreach (var  treeListObject in treeView)
        //{
        //    if (treeListObject.Child == TreeView1.SelectedNode.Text)
        //    {

        //    }
        //}

        //treeNode.ChildNodes.Remove(new TreeNode(TreeView1.SelectedNode.Value,  TreeView1.SelectedNode.Text));


        //TreeView1.Nodes.Add(treeNode);

        //Dictionary<string, string> temp = new Dictionary<string,string>();

        //objTreeList.Parent.Add(TreeView1.SelectedNode.Parent.Value, TreeView1.SelectedNode.Parent.Text);

        //temp.Add(TreeView1.SelectedNode.Value,  TreeView1.SelectedNode.Text);
        //objTreeList.Child.Add(temp);

        //newLstTree.Remove(objTreeList);           
    }
}

[Serializable]
class TreeList : IComparable
{
    public Dictionary<string, string> Parent { get; set; }

    public List<Dictionary<string, string>> Child { get; set; }

    #region IComparable Members

    public int CompareTo(object obj)
    {
        throw new NotImplementedException();
    }

    #endregion        
}
 }

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

蓝颜夕 2024-11-09 16:29:21
TreeNode tn = TreeView1.FindNode("Nodepath"); // find particular node
TreeView1.Remove(tn); // then remove from TreeView
TreeNode tn = TreeView1.FindNode("Nodepath"); // find particular node
TreeView1.Remove(tn); // then remove from TreeView
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文