我有一个ListBox,选择模式为多个.在后面的代码中,我想将一些值设置为选中.这些值存在于名为 'Names' 的 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[] Names 包含 'ragu' 和 'raju'.现在,当页面加载时,ListBox 应该包含 'ragu' 和 'raju' 作为选定值.
ListItem[] Names contains 'ragu' and 'raju'. Now, when the page loads, the ListBox should contain 'ragu' and 'raju' as selected values.
如何设置 Selected
- ListItem
?
var names = new List<string>(new string[] { "ragu", "raju" });
foreach (var item in lbto.Items)
{
if (names.Contains(item.Text))
item.Selected = true;
}
这篇关于如何设置列表框中选择的多个项目?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!