获得从ASP ListBox中所有选定值 [英] Getting all selected values from an ASP ListBox

查看:627
本文介绍了获得从ASP ListBox中所有选定值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有了将SelectionMode设置为多的ASP列表框。有retreiving所有选中的元素和任何方式不只是最后一个?

I have an ASP ListBox that has the SelectionMode set to "Multiple". Is there any way of retreiving ALL selected elements and not just the last one?

<asp:ListBox ID="lstCart" runat="server" Height="135px" Width="267px" SelectionMode="Multiple"></asp:ListBox>

使用 lstCart.SelectedIndex 只是返回的最后一个元素(如预期)。有什么会给我所有的选择?

Using lstCart.SelectedIndex just returns the last element (as expected). Is there something that will give me all selected?

这是一个Web表单。

推荐答案

您可以使用ListBox.GetSelectedIndices方法并遍历结果,然后通过访问项集合各一个。或者,也可以遍历所有的项目,并检查他们的<一个href=\"http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.listitem.selected.aspx\">Selected物业。

You can use the ListBox.GetSelectedIndices method and loop over the results, then access each one via the items collection. Alternately, you can loop through all the items and check their Selected property.

// GetSelectedIndices
foreach (int i in ListBox1.GetSelectedIndices())
{
    // ListBox1.Items[i] ...
}

// Items collection
foreach (ListItem item in ListBox1.Items)
{
    if (item.Selected)
    {
        // item ...
    }
}

// LINQ over Items collection (must cast Items)
var query = from ListItem item in ListBox1.Items where item.Selected select item;
foreach (ListItem item in query)
{
    // item ...
}

// LINQ lambda syntax
var query = ListBox1.Items.Cast<ListItem>().Where(item => item.Selected);

这篇关于获得从ASP ListBox中所有选定值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆