By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,952 Members | 1,937 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,952 IT Pros & Developers. It's quick & easy.

How to select mult items in listbox from backend code(VB)?

P: 3
I am using VB. How to select mult iteams in listbox using backend code?

I have a listbox where user can select muti items.

Expand|Select|Wrap|Line Numbers
  1.     <asp:ListBox ID="lb" SelectionMode="multiple" runat="server"  DataValueField="dv">
  2.         <asp:ListItem>red r</asp:ListItem>
  3.         <asp:ListItem>blue b</asp:ListItem>
  4.         <asp:ListItem>green g</asp:ListItem>
  5.     </asp:ListBox>
How can I set so the value of "blue b" and "green g" is already selected? I tried setSelected but this method is not supported.

Expand|Select|Wrap|Line Numbers
  1. lb.SetSelected(1, True)
  2. lb.SetSelected(2, True)
I also tried this below which kind of works. It does select 1 value but I need to able to select multi values.

Expand|Select|Wrap|Line Numbers
  1. lb.Text = "blue b"
  2. lb.Text = "green g"

I tried this also and it doesn't select any values.

Expand|Select|Wrap|Line Numbers
  1. lb.Text = "blue b green g"
May 20 '16 #1
Share this Question
Share on Google+
1 Reply


100+
P: 299
This worked for me:
Expand|Select|Wrap|Line Numbers
  1. ListBox1.SelectionMode = SelectionMode.MultiExtended
  2. ListBox1.SetSelected(0, True)
  3. ListBox1.SetSelected(2, True)
You need to make sure that the SelectionMode property is set to allow you to actually select more than a single item.
May 20 '16 #2

Post your reply

Sign in to post your reply or Sign up for a free account.