Treeview与复选框 [英] Treeview with checkboxs

查看:101
本文介绍了Treeview与复选框的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有树视图,我在每个treenode上放了一个复选框。我写了treeview_treenodeCheckChanged事件,如果选中了父节点,则检查其子节点。但是首先这个事件不起作用,然后我用Google搜索并发现我应该使用回发方法成功举办此活动。现在活动有效,但我不希望每次点击都刷新我的项目。请帮助我..



我试过的:



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>

推荐答案

你真的,真的不想要那样做。在网站中,您的C#代码总是 - 始终 - 在服务器而不是客户端上执行,这意味着如果您在C#中处理用户界面项,则数据具有进行往返服务器并返回以更新显示。这意味着两端连接到互联网的速度控制着用户在看到结果之前的行动速度 - 用户越多,得到的速度越慢......



你真的,真的,不应该在服务器上这样做(即使你使用Ajax来防止整页回发) - 你应该在本地,用javascript。



从这里开始:如何使用javascript检查.check treeview父母和孩子? | ASP.NET论坛 [ ^ ]如果这样做无效,请开始阅读:在树视图中检查父级javascript - Google搜索 [ ^ ]
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[^]


这篇关于Treeview与复选框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆