Solved

asp.net - select items in a listbox in code

Posted on 2010-11-18
3
434 Views
Last Modified: 2012-06-21
Is there a way to select items in a listbox without having to loop through the whole list?

     
      For Each item As ListItem In lstBxBrand.Items
            If item.Text = "Dell" Or item.Text = "HP" Or item.Text = "Sony" Then
                item.Selected = True
            End If
        Next


Thank you.

0
Comment
Question by:Rick
3 Comments
 
LVL 18

Accepted Solution

by:
ChetOS82 earned 500 total points
ID: 34168578
No, that is how it is done.
0
 
LVL 33

Expert Comment

by:jppinto
ID: 34168617


Public Shared Sub SetSelected(ByVal objControl As ListBox, ByVal val As String)
        Dim oDR As DataRowView
        Dim i As Integer
        For i = 0 To objControl.Items.Count - 1
            If (objControl.Items(i).Value = "Dell") Or (objControl.Items(i).Value = "HP") Or (objControl.Items(i).Value = "Sony") Then
                objControl.Items(i).Selected = True
            End If
        Next
    End Sub

Open in new window

0
 
LVL 11

Expert Comment

by:MajorBigDeal
ID: 34169014
ListItem li = ListBox1.Items.FindByText("Dell"); // also FindByValue
li.Selected = true;
or
ListBox1.Items[5].Selected = true;
or
ListBox1.SelectedIndex = 5;
0

Featured Post

Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction This article shows how to use the open source plupload control to upload multiple images. The images are resized on the client side before uploading and the upload is done in chunks. Background I had to provide a way for user…
Problem Hi all,    While many today have fast Internet connection, there are many still who do not, or are connecting through devices with a slower connect, so light web pages and fast load times are still popular.    If your ASP.NET page …
This video shows how to quickly and easily add an email signature for all users on Exchange 2016. The resulting signature is applied on a server level by Exchange Online. The email signature template has been downloaded from: www.mail-signatures…
Email security requires an ever evolving service that stays up to date with counter-evolving threats. The Email Laundry perform Research and Development to ensure their email security service evolves faster than cyber criminals. We apply our Threat…

831 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question