我有一个webpart,我想在列表框中选择多个项目,非常简单。我正在使用webcontrols namespace.So我声明列表框为 ListBox lBox = new ListBox(); lBox.ID = “lbox”; lBox.SelectionMode = “多”;
但它不接受这一点。我得到的错误是无法将字符串类型转换为列表框选择......
如果有人知道我哪里出错了?
谢谢,
答案 0 :(得分:1)
尝试: lBox.SelectionMode = ListSelectionMode.Multiple;
答案 1 :(得分:1)
来自Programmatically Select Multiple Items
<div>
<asp:ListBox ID="ListBox1" runat="server">
<asp:ListItem Value="One" />
<asp:ListItem Value="Two" />
<asp:ListItem Value="Three" />
<asp:ListItem Value="Four" />
<asp:ListItem Value="Five" />
</asp:ListBox></div>
</div>
代码背后:
protected void Page_Load(object sender, EventArgs e)
{
ListBox1.SelectionMode = System.Web.UI.WebControls.ListSelectionMode.Multiple;
for (int i = 0; i < ListBox1.Items.Count; i++)
{
// Select the first, third and fifth items in the listbox
if(i == 0 || i == 2 || i == 4)
{
ListBox1.Items[i].Selected = true;
}
}
}
答案 2 :(得分:1)
试试这个:
ListBox l = new ListBox();
l.SelectionMode = ListSelectionMode.Multiple;