TreeView的子节点和父节点级联选中,遍历TreeView并查找和选定节点

 TreeView的子节点和父节点级联选中

参考:(TreeView的子节点和父节点级联选中)http://blog.sina.com.cn/s/blog_56616d970100c483.html

(TreeView点击文字选中和点击父节点选中子节点 )http://hi.baidu.com/lhlsxdx/blog/item/44988f8f95f006e4f01f3691.html

(C#中遍历TreeView并查找和选定节点 )http://apps.hi.baidu.com/share/detail/616013

aspx:

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title>测试树的级联选中和取消</title>
    <script type="text/javascript">
    function postBackByObject()
    {
        var obj = window.event.srcElement;
        if(obj.tagName == "INPUT" && obj.type == "checkbox")
        {
            __doPostBack("","");
        }
    }

    </script>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <asp:TreeView ID="TreeView1" runat="server" OnTreeNodeCheckChanged="TreeView1_TreeNodeCheckChanged"
            ShowCheckBoxes="All" ShowLines="True" onselectednodechanged="TreeView1_SelectedNodeChanged"></asp:TreeView>
    </div>
    </form>
</body>
</html>

后台代码:

protected void Page_Load(object sender, EventArgs e)
        {
            if(!IsPostBack)
            {
                this.TreeView1.Attributes.Add("onclick", "postBackByObject()");
                TreeView1.Attributes.Add("onselectednodechanged", "TreeView1_SelectedNodeChanged");
				Bind();
            }
}
		
		
	void Bind()
        {
List<ContactGroupDomain> nodes = (List<ContactGroupDomain>)Helper.ContactGroup().GetAllGroup(uid);
            if(null == nodes)
            {
                return;
            }
            for(int i = 0; i < nodes.Count; i++)//按节点数量循环
            {
                TreeNode node = new TreeNode();
                node.Value = nodes[i].CGID;
                node.Text = nodes[i].CGName;
                BindChildNode(node);
                TreeView1.Nodes.Add(node);
            }
        }

        void BindChildNode(TreeNode node)
        {
            List<ContactPersonDomain> personList = (List<ContactPersonDomain>)Helper.ContactPerson().GetContactByGID(uid, node.Value);
            for(int i = 0; i < personList.Count; i++)
            {
                TreeNode childNode = new TreeNode();
                childNode.Value = personList[i].CPID;
                childNode.Text = personList[i].CPName;
                node.ChildNodes.Add(childNode);
            }
        }

        

 protected void TreeView1_TreeNodeCheckChanged(object sender, TreeNodeEventArgs e)         {             SetChildChecked(e.Node);             SetParentChecked(e.Node);         }

        /// <summary>         /// 选中父节点,其所有子节点也选中         /// </summary>         /// <param name="parentNode"></param>         private void SetChildChecked(TreeNode parentNode)         {             if(parentNode.Checked == true)             {                 foreach(TreeNode tn in parentNode.ChildNodes)                 {                     tn.Checked = parentNode.Checked;                     if(tn.ChildNodes.Count > 0)                     {                         SetChildChecked(tn);                     }                 }             }             else             {                 foreach(TreeNode tn in parentNode.ChildNodes)                 {                     tn.Checked = parentNode.Checked;                     if(tn.ChildNodes.Count > 0)                     {                         SetChildChecked(tn);                     }                 }             }         }

        /// <summary>         /// 选中子节点,其父节点也选中         /// </summary>         /// <param name="childNode"></param>         private void SetParentChecked(TreeNode childNode)         {             if(childNode.Checked == true)             {                 if(childNode.Parent != null)                 {                     childNode.Parent.Checked = childNode.Checked;                     SetParentChecked(childNode.Parent);                 }             }             else             {                 if(childNode.Parent != null)                 {                     if(!CheckChildAllCheck(childNode.Parent))                     {                         childNode.Parent.Checked = childNode.Checked;                         SetParentChecked(childNode.Parent);                     }                 }             }         }

        /// <summary>         /// 判断当前节点的兄弟节点及其子节点有没有选中的,如果有,直接返回true         /// 如果没有,继续递归。         /// </summary>         /// <param name="node"></param>         /// <returns></returns>         private bool CheckChildAllCheck(TreeNode node)         {             bool isCheck = false;             foreach(TreeNode childNode in node.ChildNodes)             {                 if(childNode.Checked)                 {                     return true;                 }                 else                 {                     isCheck = CheckChildAllCheck(childNode);                 }             }             return isCheck;         }

protected void TreeView1_SelectedNodeChanged(object sender, EventArgs e) { TreeNode treeNode = TreeView1.SelectedNode; }

程序中的对象集合大家根据自己的实际需要更改。

效果图:


选中“三级节点1”,它的的所有父节点都会选中。

 

 

C#中遍历TreeView并查找和选定节点

首先先看一段MSDN上的示例程序:打印一个树中所有节点名称
private void PrintRecursive(TreeNode treeNode)
{
    // Print the node.
     System.Diagnostics.Debug.WriteLine(treeNode.Text);
     MessageBox.Show(treeNode.Text);
    // Print each node recursively.
   foreach (TreeNode tn in treeNode.Nodes)
    {
         PrintRecursive(tn);
     }
}

// Call the procedure using the TreeView.
private void CallRecursive(TreeView treeView)
{
    // Print each node recursively.
     TreeNodeCollection nodes = treeView.Nodes;
    foreach (TreeNode n in nodes)
    {
         PrintRecursive(n);
     }
}

然后要说明的是一下TreeView类和TreeNode类之间的关系:TreeView类中有个只读属性是Nodes,它是属于TreeNodeCollection类型的,而对于一个TreeView它的Nodes属性就是返回treeView根结点的集合。

然后就是我的递归遍历查找一个树节点的方法(由于程序需要我是根据树节点的ImageIndex属性查找的):

private TreeNode FindTreeNode(int imageIndex, TreeNode tnParent)
{
    if (tnParent == null)
        return null;
    if (tnParent.ImageIndex == imageIndex)
        return tnParent;
     TreeNode tnRet = null;
    foreach (TreeNode tn in tnParent.Nodes)
     {
         tnRet = FindTreeNode(imageIndex, tn);
        if (tnRet != null)
            break;
     }
    return tnRet;
}

private TreeNode CallFindNode(int imageIndex, TreeView treeView)
{
     TreeNodeCollection nodes = treeView.Nodes;
    foreach (TreeNode n in nodes)
     {
         TreeNode temp = FindTreeNode(imageIndex, n);
        if (temp != null)
            return temp;
     }
    return null;
}

/// <summary>
/// 这个是一个点击事件,其中改变了选中节点,
/// 改变选中节点的同时就触发了selectedNodeChanged事件
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
void myControlTransactions_AddCustomer(object sender, EventArgs e)
{
     TreeNode temp = CallFindNode(3, treeView1);
    if (temp != null)
         treeView1.SelectedNode = temp;
}



 

 

阅读更多
个人分类: ASP.NET
想对作者说点什么? 我来说一句

没有更多推荐了,返回首页

关闭
关闭
关闭