ASP.NET 的 CheckBoxList 没有 SelectedItems 成员是否有原因?

发布于 2024-09-24 23:27:00 字数 128 浏览 5 评论 0原文

每当我想要获取选定的项目时,我必须循环遍历每个项目并查看它是否被选中。他们甚至有一个 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

浅听莫相离 2024-10-01 23:27:00

因为他们不是在 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.)

奶茶白久 2024-10-01 23:27:00

除了达斯汀的 en drachenstern 的答案。你可以自己动手:-)

public static IEnumerable<ListItem> SelectedItems(this CheckBoxList cbl)
{
    return cbl.Items.Cast<ListItem>().Where(l=>l.Selected == true);
}

In addition to Dustin's en drachenstern's answers. You can roll your own :-)

public static IEnumerable<ListItem> SelectedItems(this CheckBoxList cbl)
{
    return cbl.Items.Cast<ListItem>().Where(l=>l.Selected == true);
}
红焚 2024-10-01 23:27:00

我不得不说,因为复选框列表呈现单独的 HTML 复选框,这些复选框不能像单选按钮那样分组,所以需要逐项评估所选属性。

它是文档的一部分。另请注意,SelectedIndex 将返回带有 最低索引

CheckBoxList 控件提供了
多选复选框组
可以根据数据动态生成
绑定。它包含一个项目
与成员对应的集合
到列表中的各个项目。到
确定检查哪些项目,
迭代集合并
测试每个的 Selected 属性
列表中的项目。

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.

The CheckBoxList control provides a
multi selection check box group that
can be dynamically generated with data
binding. It contains an Items
collection with members corresponding
to individual items in the list. To
determine which items are checked,
iterate through the collection and
test the Selected property of each
item in the list.

初相遇 2024-10-01 23:27:00

子类化 CheckBoxList 并自己实现此功能也相当容易,然后您可以重复使用。

public class ExtendedCheckBoxList : CheckBoxList
{
    public List<string> SelectedItems
    {
        get
        {
            return (from ListItem item in Items
                    where item.Selected
                    select item.Value).ToList();
        }
    }
}

It's also fairly easy to subclass the CheckBoxList and implement this functionality yourself, which you can then re-use.

public class ExtendedCheckBoxList : CheckBoxList
{
    public List<string> SelectedItems
    {
        get
        {
            return (from ListItem item in Items
                    where item.Selected
                    select item.Value).ToList();
        }
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文