开发者

Problem in selecting multiple items in Listbox.SelectionMode

I have a webpart and I want to select mulitple items in a listbox,pretty easy.I开发者_运维百科 am using webcontrols namespace.So I am declaring listbox as ListBox lBox = new ListBox(); lBox.ID="lbox"; lBox.SelectionMode="Multiple";

But it is not accepting that . The error I m getting is cannot convert string type to listbox selection sth..

If anyone is having any idea where I m getting wrong?

Thanks,


From 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> 

Code behind:

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; 
      }
   }
}


Try: lBox.SelectionMode = ListSelectionMode.Multiple;


Try this:

ListBox l = new ListBox();
l.SelectionMode = ListSelectionMode.Multiple;
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜