Treeview节点已检查

Treeview节点已检查

本文介绍了C#.NET Treeview节点已检查的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个带有多个节点和子节点的树视图控件(带复选框)。

当我点击父节点时,我需要选择其下的所有子节点。

SelectedNodeChanged& TreeNodeCheckChanged事件没有被解雇。



I have a treeview control(with checkboxes) with several nodes and child nodes.
When I click on a parent node, I need all the child nodes under that to be selected.
SelectedNodeChanged & TreeNodeCheckChanged events are not getting fired.

protected void tvwPhases_SelectedNodeChanged(object sender, EventArgs e)
{
    for (int iCnt = 0; iCnt < tvwPhases.Nodes.Count; iCnt++)
    {
        if ((tvwPhases.Nodes[iCnt].ChildNodes.Count > 0) && (tvwPhases.Nodes[iCnt].Checked == true))
        {
            for (int jCnt = 0; jCnt < tvwPhases.Nodes[iCnt].ChildNodes.Count; jCnt++)
            {
                tvwPhases.Nodes[iCnt].ChildNodes[jCnt].Checked = true;
            }
        }
    }
}





对此有任何帮助都非常感谢。



Any helps on this are highly appreciated.

推荐答案

<br />
<pre><br />
// event handler ...<br />
this.treeView1.AfterCheck += new System.Windows.Forms.TreeViewEventHandler(this.treeView1_AfterCheck);<br />
<br />
// <summary><br />
// Handles the AfterCheck event of the treeLocation control.<br />
// </summary><br />
private void treeView1_AfterChecktreeLocation_AfterCheck(object sender, TreeViewEventArgs e)<br />
{<br />
....<br />
}<br />
</pre><br />





- 或者 -





-- OR --

<br />
<pre><br />
// Updates all child tree nodes recursively.<br />
private void CheckAllChildNodes(TreeNode treeNode, bool nodeChecked)<br />
{<br />
   foreach(TreeNode node in treeNode.Nodes)<br />
   {<br />
      node.Checked = nodeChecked;<br />
      if(node.Nodes.Count > 0)<br />
      {<br />
         // If the current node has child nodes, call the CheckAllChildsNodes method recursively.<br />
         this.CheckAllChildNodes(node, nodeChecked);<br />
      }<br />
   }<br />
}<br />
// NOTE   This code can be added to the BeforeCheck event handler instead of the AfterCheck event.<br />
// After a tree node's Checked property is changed, all its child nodes are updated to the same value.<br />
private void node_AfterCheck(object sender, TreeViewEventArgs e)<br />
{<br />
   // The code only executes if the user caused the checked state to change.<br />
   if(e.Action != TreeViewAction.Unknown)<br />
   {<br />
      if(e.Node.Nodes.Count > 0)<br />
      {<br />
         /* Calls the CheckAllChildNodes method, passing in the current <br />
         Checked value of the TreeNode whose checked state changed. */<br />
         this.CheckAllChildNodes(e.Node, e.Node.Checked);<br />
      }<br />
   }<br />
}<br />
</pre><br />





- 或者 -



如果您正在使用ASP.NET,请查看以下帖子:



[更新]



亲切问候,



-- OR --

If your'e making use of ASP.NET, then have a look at the following post: Check all related childNodes

[UPDATE]

Kind regards,



Hey, it works on me, please try this:

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm1.aspx.cs" Inherits="TestWebDemo.WebForm1" %>

<!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>Table Demo</title>
    <script type="text/javascript">

     function postBackByObject()
     {
         var o = window.event.srcElement;
         if (o.tagName == "INPUT" && o.type == "checkbox")
        {
           __doPostBack("","");
        }
    }
   </script>

</head>
<body>
    <form id="form1" runat="server">
    <div>
        <asp:TreeView ID="TreeView1" runat="server"

            ontreenodecheckchanged="TreeView1_TreeNodeCheckChanged">
            <Nodes>
                <asp:TreeNode ShowCheckBox="True" Text="New Node 1" Value="New Node">
                </asp:TreeNode>
                <asp:TreeNode ShowCheckBox="True" Text="New Node" Value="New Node">
                </asp:TreeNode>
                <asp:TreeNode ShowCheckBox="True" Text="New Node" Value="New Node">
                </asp:TreeNode>
                <asp:TreeNode ShowCheckBox="True" Text="New Node" Value="New Node">
                </asp:TreeNode>
                <asp:TreeNode ShowCheckBox="True" Text="New Node" Value="New Node">
                </asp:TreeNode>
                <asp:TreeNode ShowCheckBox="True" Text="New Node" Value="New Node">
                </asp:TreeNode>
            </Nodes>
        </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.Web.UI.HtmlControls;
using System.Data;

namespace TestWebDemo
{
    public partial class WebForm1 : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            TreeView1.Attributes.Add("onclick", "postBackByObject()");
        }

        protected void TreeView1_TreeNodeCheckChanged(object sender, TreeNodeEventArgs e)
        {
            if (e.Node.Text == "New Node 1")
            {
                ClientScript.RegisterStartupScript(GetType(), "alertme", "alert('The first node is selected...');", true);
            }
        }
    }
}


这篇关于C#.NET Treeview节点已检查的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-16 01:15