我有一个 web 部件,我想在列表框中选择多个项目,非常简单。我正在使用 webcontrols 命名空间。所以我将列表框声明为 ListBox lBox = new ListBox(); lBox.ID="lbox"; lBox.SelectionMode="多个";
但它不接受这一点。我收到的错误是无法将字符串类型转换为列表框选择......
如果有人知道我哪里出错了?
谢谢
最佳答案
来自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;
}
}
}
关于c# - 在 Listbox.SelectionMode 中选择多个项目时出现问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2093634/