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

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • ikhlas
    New Member
    • May 2016
    • 3

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

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

    I have a listbox where user can select muti items.

    Code:
        <asp:ListBox ID="lb" SelectionMode="multiple" runat="server"  DataValueField="dv">
            <asp:ListItem>red r</asp:ListItem>
            <asp:ListItem>blue b</asp:ListItem>
            <asp:ListItem>green g</asp:ListItem>
        </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.

    Code:
    lb.SetSelected(1, True)
    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.

    Code:
    lb.Text = "blue b"
    lb.Text = "green g"

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

    Code:
    lb.Text = "blue b green g"
  • Luk3r
    Contributor
    • Jan 2014
    • 300

    #2
    This worked for me:
    Code:
    ListBox1.SelectionMode = SelectionMode.MultiExtended
    ListBox1.SetSelected(0, True)
    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.

    Comment

    Working...