Solved

asp.net - select items in a listbox in code

Posted on 2010-11-18
3
433 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

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…
Parsing a CSV file is a task that we are confronted with regularly, and although there are a vast number of means to do this, as a newbie, the field can be confusing and the tools can seem complex. A simple solution to parsing a customized CSV fi…
When you create an app prototype with Adobe XD, you can insert system screens -- sharing or Control Center, for example -- with just a few clicks. This video shows you how. You can take the full course on Experts Exchange at http://bit.ly/XDcourse.
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …

948 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

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now