如何设置多个在ListBox中选择的项目? [英] How to set multiple items as selected in ListBox?

查看:100
本文介绍了如何设置多个在ListBox中选择的项目?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 ListBox ,具有多个选择模式.在后面的代码中,我要设置一些选定的值.这些值出现在名为名称" ListItems [] 中.

I have one ListBox with selection mode of multiple. In code behind, I want to set some values as selected. These values are present in a ListItems[] named 'Names'.

HTML代码:

<asp:ListBox ID="lbto" class="chosen" runat="server" Width="450px" 
 Height="20px" SelectionMode="Multiple">
    <asp:ListItem>Mandy</asp:ListItem>
    <asp:ListItem>Amit</asp:ListItem>
    <asp:ListItem>sundar</asp:ListItem>
    <asp:ListItem>ragu</asp:ListItem>
    <asp:ListItem>raju</asp:ListItem>
</asp:ListBox>

ListItem []名称包含'ragu''raju'.现在,在页面加载时,列表框应包含'ragu''raju'作为选定值.

ListItem[] Names contains 'ragu' and 'raju'. Now, when the page loads, the ListBox should contain 'ragu' and 'raju' as selected values.

推荐答案

如何设置 Selected -

What about setting the Selected-property of the ListItem?

var names = new List<string>(new string[] { "ragu", "raju" });

foreach (var item in lbto.Items)
{
    if (names.Contains(item.Text))
        item.Selected = true;
}

这篇关于如何设置多个在ListBox中选择的项目?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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