开发者

I am using collection to populate treeview in asp.net. How i will delete node from tree

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开发者_StackOverflow社区 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        
}
 }


TreeNode tn = TreeView1.FindNode("Nodepath"); // find particular node
TreeView1.Remove(tn); // then remove from TreeView
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜