Click here to Skip to main content
15,887,434 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have treeview and I put a checkbox each treenode.I write treeview_treenodeCheckChanged event for if the parentnode is checked, its childnodes be checked.But firstly this event didn't work, then I googled and find that I should use postback method for this event successfully.Now event works but I don't want my project is refresh in each click.Pleasee help me..

What I have tried:

protected void TreeView1_TreeNodeCheckChanged(object sender, TreeNodeEventArgs e)
        {
            if (e.Node.ChildNodes.Count > 0 && e.Node.Checked)
            {
                foreach (TreeNode childnode in e.Node.ChildNodes)
                {
                    childnode.Checked = true;
                }
            }
            else if (e.Node.ChildNodes.Count > 0 && !e.Node.Checked)
            {
                foreach (TreeNode childnode in e.Node.ChildNodes)
                {
                    childnode.Checked = false;
                }
            }
            else if (e.Node.ChildNodes.Count == 0 && e.Node.Checked)
            {
                e.Node.Parent.Checked = true;
            }
            else if (e.Node.ChildNodes.Count == 0 && Count_checkedChilds(e.Node.Parent) == 0)
            {
                e.Node.Parent.Checked = false;
            }
        }

protected int Count_checkedChilds(TreeNode node)
        {
            int k = 0;
            foreach (TreeNode childnode in node.ChildNodes)
            {
                if (childnode.Checked == true)
                {
                    k++;
                }
            }
            return k;
        }




 <script type="text/javascript">
        function postBackByObject() {
            var o = window.event.srcElement;
            if (o.tagName == "INPUT" && o.type == "checkbox") {
                __doPostBack('TreeView1', '');
            }
        }
</script>
Posted
Updated 20-Feb-19 22:31pm

1 solution

You really, really don't want to do that. In a website, your C# code is always - always - executed on the server not the client, and that means that if you handle user interface items in C#, the data has to make a round trip to the server and back to update the display. That's means that the speed of the connection to to the internet at both ends controls how fast the user's action will take before he sees a result - and the more users you have, the slower that gets ...

You really, really, should not be doing this at the server (even if you use Ajax to prevent a whole page postback) - you should do this locally, in javascript.

Start here: how to Check.uncheck treeview parent and child using javascript? | The ASP.NET Forums[^] and if that doesn't help, start reading: checking parent in treeview javascript - Google Search[^]
 
Share this answer
 

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