ASP.NET 的 CheckBoxList 没有 SelectedItems 成员是否有原因?
每当我想要获取选定的项目时,我必须循环遍历每个项目并查看它是否被选中。他们甚至有一个 SelectedItem(末尾没有“s”)成员,这对于 CheckBoxList 来说似乎很奇怪。这似乎是一个合乎逻辑的事情,有谁知道为什么他们没有添加它?
Whenever I want to get selected items I have to loop through each item and see if it's selected. They even have a SelectedItem (no "s" at the end) member which seems odd for a CheckBoxList. It seems like a logical thing to have, does anyone know why they haven't added it?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
因为他们不是在 CheckBoxList 中实现 SelectedItem,而是在 CheckBoxList 继承的 ListControl 中实现。可以说,CheckBoxList 需要追溯到源代码,因为它的许多编写方式并不“正确”,但这是一个主观论点。 (这是个人咆哮的主题,我刚刚遇到了太多 CheckBoxList 愚蠢地做某事的情况,这很烦人,仅此而已。只是不是我想的思维方式,并且从未有其他人证实它是对他们来说也很烦人。)
Because they're not implementing the SelectedItem in CheckBoxList, but in ListControl that CheckBoxList inherits from. It can be argued that CheckBoxList needs to be taken back to source, as many of the ways that it is written are just not "correct" but that's a subjective argument. (this is the subject of a personal rant, I've just come across too many cases of CheckBoxList doing something obtusely and it's annoying, is all. Just not the way my mind works I suppose, and have never had others corroborate that it is annoying to them too.)
除了达斯汀的 en drachenstern 的答案。你可以自己动手:-)
In addition to Dustin's en drachenstern's answers. You can roll your own :-)
我不得不说,因为复选框列表呈现单独的 HTML 复选框,这些复选框不能像单选按钮那样分组,所以需要逐项评估所选属性。
它是文档的一部分。另请注意,
SelectedIndex
将返回带有 最低索引。I would have to say that because the checkbox list renders individual HTML checkboxes that are not groupable like radio buttons the selected property needs to be evaluated on a item by item basis.
It is part of the documentation. Also note that the
SelectedIndex
will return the item with the lowest index.子类化 CheckBoxList 并自己实现此功能也相当容易,然后您可以重复使用。
It's also fairly easy to subclass the CheckBoxList and implement this functionality yourself, which you can then re-use.