如何在asp.net中查找复选框列表是否被选中

发布于 2024-12-13 16:57:34 字数 220 浏览 6 评论 0原文

如何在 asp.net 中的复选框列表中获取选定的索引。我应该循环查找列表框是否被选中,或者我可以不这样做就知道吗?我想这样做

如果(选中复选框列表) {做这个} 别的 {do this}

如何在asp.net中查找复选框列表是否被选中

int roleselected = ckl_EditRole.Items.SelectedIndex;

How to get selected index in a check box list in asp.net. Should I loop through to find whether the list box is selected or can i get to know without doing that. I want to do this

if(Checkboxlist selected)
{do this}
else
{do this}

how to find if the check box list is selected or not in asp.net

int roleselected = ckl_EditRole.Items.SelectedIndex;

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(2

若无相欠,怎会相见 2024-12-20 16:57:34

对于 CheckBoxList,SelectedIndex 只会为您提供 CheckBoxList 中第一个选定的索引。如果不是 -1,则表明已选择某些内容。这可能足以满足您的需求。

if( ckl_EditRole.SelectedIndex != -1 )
{
// Do Something
}

但是,由于 CheckBoxList 可以有多个选择,因此您可能希望循环遍历项目并查找选定的项目。

foreach( ListItem li in ckl_EditRole.Items )
{
    if( li.Selected )
    {
        // Do Something
    }
}

For CheckBoxList, SelectedIndex will give you just the first selected index in the CheckBoxList. If it's not -1, then something was selected. This may be enough for what you're looking for.

if( ckl_EditRole.SelectedIndex != -1 )
{
// Do Something
}

But, since the CheckBoxList can have multiple selections, you probably want to loop through the Items and look for the selected ones.

foreach( ListItem li in ckl_EditRole.Items )
{
    if( li.Selected )
    {
        // Do Something
    }
}
北音执念 2024-12-20 16:57:34

如果您的目的是获取代码给出的所选复选框的索引,您也可以通过 Linq(不带 forloop)来实现此目的,如下所示。

ckl_EditRoleItems.OfType<ListItem>().Where(p=>p.Selected).Select(p => ckl_EditRoleItems.Items.IndexOf(p)).ToArray<int>();

该语句将返回一个 int 数组,其中包含所选复选框的索引。

If your intention is to get index of the selected checkbox as given by your code, you can also achieve this via Linq(without forloop) as below.

ckl_EditRoleItems.OfType<ListItem>().Where(p=>p.Selected).Select(p => ckl_EditRoleItems.Items.IndexOf(p)).ToArray<int>();

This statement will return an array of int which will contain the index of the check boxes that are selected.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文