Click here to Skip to main content
15,883,705 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
See more:
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.

C#
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.
Posted
Updated 15-May-11 23:48pm
v2
Comments
Hemant__Sharma 16-May-11 5:40am    
Add some code here.

Hi,

Instead of making use of the SelectedNodeChanged & TreeNodeCheckChanged events, try the TreeView.AfterCheck Event: More Information. I made use of that event in one of my projects to accomplish the exact same thing and it worked for me...

[UPDATE]

Okay, the TreeView.AfterCheckEvent event is located within the Events list of the Treeview. The first event under the 'Behaviour' section.

// event handler ...
this.treeView1.AfterCheck += new System.Windows.Forms.TreeViewEventHandler(this.treeView1_AfterCheck);

// <summary>
// Handles the AfterCheck event of the treeLocation control.
// </summary>
private void treeView1_AfterChecktreeLocation_AfterCheck(object sender, TreeViewEventArgs e)
{
....
}


-- OR --

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


-- OR --

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

[UPDATE]

Kind regards,
 
Share this answer
 
v5
Comments
Prabhu_Sundaram 16-May-11 8:56am    
thanks for your post..

I don't see AfterCheck event.
Should I add any EventHandler in specific.

I just see the following and few other events..

OnTreeNodeCheckChanged, OnSelectedNodeChanged, OnTreeNodeCheckChanged, OnTreeNodeDataBound, OnPreRender, OnDataBound

it would be great if you could share some piece of your code.
thanks in advance..
Programm3r 16-May-11 9:32am    
I have updated the solution ...
Prabhu_Sundaram 20-May-11 5:29am    
thanks for your posts..
this one worked for me..

http://geekswithblogs.net/ranganh/archive/2009/01/21/updated-asp.net-treeview-checkboxes-ndash-check-all-ndash-javascript.aspx
ashishdhyani90 23-Apr-15 7:28am    
yui
Programm3r 16-May-11 9:34am    
Is this an asp.net or win forms application??
XML
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);
            }
        }
    }
}
 
Share this answer
 
Comments
Deepu S Nair 21-Jan-15 6:55am    
Please don't answer old or already solved questions.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900