c# - 在 Listbox.SelectionMode 中选择多个项目时出现问题

标签 c# asp.net

我有一个 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/

相关文章:

c# - 多线程环境下 SendOrPostCallback 和 Action 的区别?

javascript - 无法将参数传递给谷歌饼图MySql查询

asp.net - 让 session 持续更长时间

c# - 对 Winform 的叠加控制

c# - 添加 UITabBarController 时出错

c# - 使用 Process Class c# 列出进程内存和 CPU 使用情况时出现问题

C# 在条件语句中可为空

html - 当我将鼠标指针悬停在链接上时如何完全填充导航栏高度?

javascript - ASP.net、JavaScript/JQuery - 服务器发送事件 (SSE) 无法与 ASHX 处理程序一起使用

c# - 是否可以从后面的代码编辑元描述?