Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

asp.net - select items in a listbox in code

Posted on 2010-11-18
3
Medium Priority
?
444 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 2000 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

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

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

ASP.Net to Oracle Connectivity Recently I had to develop an ASP.NET application connecting to an Oracle database.As I am doing it first time ,I had to solve several problems. This article will help to such developers  to develop an ASP.NET client…
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…
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an anti-spam), the admin…
Despite its rising prevalence in the business world, "the cloud" is still misunderstood. Some companies still believe common misconceptions about lack of security in cloud solutions and many misuses of cloud storage options still occur every day. …
Suggested Courses
Course of the Month11 days, 10 hours left to enroll

916 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