如何让Treeview只勾选一个选项
我有一个树视图,并且复选框设置为 true。我想要的是整个树视图中只选择一个复选框。我怎样才能做到这一点?
仅供参考:树视图有三层深度。
I have a treeview and Checkbox is set true. What I want is that only one checkbox is selected in the whole treeview. How can I do that?
FYI: the treeview is in three level depth.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
![扫码二维码加入Web技术交流群](/public/img/jiaqun_03.jpg)
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(8)
我遇到了同样的问题,但循环所有节点听起来对性能来说代价高昂。
这是我的解决方案,没有循环遍历所有节点,而是使用类属性:
我发现此解决方案的唯一问题是,如果用户将同一复选框切换为快速,则该函数不会被正确调用,并且不会“检查”或者单击两次后单击一次“取消选中”该框。
也许其他人可以添加修复程序,以在运行时快速单击同一复选框。
I had the same issue, but looping trough all Nodes sounds expensive for performance.
Here is my solution without looping trough all Nodes, but using a class property:
The only issue i found with this solution is that if the user toggle the same checkbox to fast, the function doesn't get called correctly and it doesnt "check" or "uncheck" the box with 1 click, just after clicking it twice then.
Maybe someone else can add a fix for clicking the same checkbox to fast at runtime.
我使用此处找到的解决方案。
页面中的 javascript
和代码后面将 client_OnTreeNodeChecked 函数添加到树视图 onclick 事件
注意:阅读我的帖子 如果 TreeView 位于更新面板
I use the solution found here.
A javascript in the page
And code behind to add client_OnTreeNodeChecked function to your treeviews onclick event
NOTE: read my post How to make javascript work along with Ajax UpdatePanel if TreeView is inside an UpdatePanel
此解决方案我无耻地偷来的它允许您一次性迭代所有级别的所有节点。
这使得确保只选择一个节点变得容易:
This solution which I shamelessly stole allows you to iterate over all nodes at all levels in one go.
This makes ensuring only one node is selected easy:
最简单的方法是为树视图的 AfterCheck 事件设置一个偶数处理程序。在此处理程序中,您可以取消选中所有节点,但刚刚选中的节点除外:
应该可以工作(没有尝试)
The simplest way to do that is to set an even handler to your tree view's AfterCheck event. In this handler you can uncheck all the nodes but the one which just became checked:
Should work (didn't try)
这是一篇旧文章,尽管提供的解决方案都不适用于我的情况。
所以我做了如下操作,
并
注意
此方法可以防止
StackOverflowException
!希望对其他人有用
It is an old post, though none of provided solutions is working in my case.
So I did as follows,
and
and
note that this method prevents
StackOverflowException
!hope useful to others
我试过了,确实有效
I tried it certainly works