Search code examples
asp.neteventstreeviewprogrammatically-created

Create TreeView with events programmatically


I am creating a tree programmatically inside a row in a table. Works fine, but can not get the assigned event is called:

TreeView arbolCapas = new TreeView();
            arbolCapas.ID = "capas";
            foreach (String capa in servicio.Capas)
            {
                TreeNode childNodes = new TreeNode();
                childNodes.Text = capa;
                childNodes.ShowCheckBox = true;
                childNodes.SelectAction = TreeNodeSelectAction.None;         
                arbolCapas.Nodes.Add(childNodes);                
            }            
            arbolCapas.SelectedNodeChanged +=new EventHandler(arbolCapas_TreeNodeCheckChanged);
            tbC.Controls.Add(arbolCapas);
            tbR.Cells.Add(tbC);

 protected void arbolCapas_TreeNodeCheckChanged(Object sender, EventArgs e)
        {
            TreeView elemento = (TreeView)(((CheckBox)sender).Parent);
             foreach (TreeNode node in elemento.CheckedNodes) 
             {                //if (node.Checked)   
             }
        }

How I can call an event when the checkbox of a child node is checked?

Thanks a lot.


Solution

  • I found the solution by adding the event as follows:

    arbolCapas.Attributes.Add("onclick", "OnCheckBoxCheckChanged(event)");
    

    And then, in javascript:

    function OnCheckBoxCheckChanged(evt) {
            alert("check change");
    }
    

    Here the solution:

    http://geekswithblogs.net/ranganh/archive/2009/01/21/updated-asp.net-treeview-checkboxes-ndash-check-all-ndash-javascript.aspx