简体   繁体   English

在 ASP.NET 2.0 中,我们如何检查列表框中的任何项目是否被选中?

[英]How do we check whether any item in the listbox is selected in ASP.NET 2.0?

I have to do a for loop and check whether any of the items are checked if I want to know if any of the items are checked.如果我想知道是否检查了任何项目,我必须执行 for 循环并检查是否检查了任何项目。

In C#, there is something like在 C# 中,有类似的东西

listbox.SelectedItems.Count();

Is there any similar method for ASP.NET? ASP.NET 有没有类似的方法?

you can see whether any item is selected by the below code:您可以通过以下代码查看是否选择了任何项目:

if (listboxname.SelectedIndex == -1)
       MessageBox.Show("Please select an Item first!");

看起来你需要遍历它们。

According to MSDN and my experience, you have to go over all the items, to do this:根据 MSDN 和我的经验,你必须检查所有项目,才能做到这一点:

you can determine the selected item(s) in the ListBox control by enumerating the Items collection and testing the Selected value for each ListItem element.您可以通过枚举 Items 集合并测试每个 ListItem 元素的 Selected 值来确定 ListBox 控件中的选定项。

This might not be available in 2.0 though...but another option is to query the Items collection with LINQ.这在 2.0 中可能不可用...但另一种选择是使用 LINQ 查询 Items 集合。 See this link for info on that有关方面的信息,请参阅此链接

I had the same issue and I may have found a solution for you.我遇到了同样的问题,我可能已经为您找到了解决方案。 I don't know how effective it is.我不知道它有多有效。 But here it is:但这里是:

if (listbox.SelectedIndex <= -1)
{
    listbox.SelectedIndex = 0;
}
if (listbox.SelectedIndex > -1)
{
    //do something
}

I had the same issue and I may have found a solution for you.我遇到了同样的问题,我可能已经为您找到了解决方案。 I don't know how effective it is.我不知道它有多有效。 But here it is:但这里是:

        if (listData.SelectedIndex>=0)
        {               
                listData.Items.Remove(listData.SelectedItem.ToString());                
        }
        else
        {
            MessageBox.Show("plz select data first");
        }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM