Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

asp.net - select items in a listbox in code

Posted on 2010-11-18
3
Medium Priority
?
442 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

Creating an analog clock UserControl seems fairly straight forward.  It is, after all, essentially just a circle with several lines in it!  Two common approaches for rendering an analog clock typically involve either manually calculating points with…
This article aims to explain the working of CircularLogArchiver. This tool was designed to solve the buildup of log file in cases where systems do not support circular logging or where circular logging is not enabled
Monitoring a network: how to monitor network services and why? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the philosophy behind service monitoring and why a handshake validation is critical in network monitoring. Software utilized …
This tutorial will teach you the special effect of super speed similar to the fictional character Wally West aka "The Flash" After Shake : http://www.videocopilot.net/presets/after_shake/ All lightning effects with instructions : http://www.mediaf…

670 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