Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Problems programmatically setting multiple selections in a list box

Status
Not open for further replies.

columbo2

Technical User
Jul 14, 2006
97
0
0
GB
Hi all,

I have an array of values that I want to select in a multi selection listbox.
I've tried this, but it only ever shows one of the selecte d values...

(c#)
foreach (string itemstoselect in arrayofitems)
{
lbJobType.SelectedValue = itemstoselect;
}

Can anyone help, I have been surfing the web but cannot find the answer.

ta
C
 
The selected value property is not built to handle multi selected items. You need to loop through the items like this.

Code:
        Dim li As Web.UI.WebControls.ListItem
        For Each li In ListBox1.Items
            If li.Value = "your test" Then
                li.Selected = True
            End If
        Next

Senior Software Developer
 
Hi , Thanks for your answer.
I can see what you're getting at, I need to cycle through each item in the listbox, compare it to each item in the array of values I want selected and select where there is a match.
The trouble is I am using c# and can't find a way to set the individual item to be selected.
Can anyone help?
thanks
 
The C# method should be exactly the same as the VB.NET method. At a guess, I'd say the syntax would be something like:
Code:
ListItem li;
foreach (li in ListBox1.Items) {
    if ((li.Value == "your test")) {
        li.Selected = true;
    }
}


------------------------------------------------------------

Mark,
[URL unfurl="true"]http://aspnetlibrary.com[/url]
[URL unfurl="true"]http://mdssolutions.co.uk[/url] - Delivering professional ASP.NET solutions
 
Hi SBO,
Thanks for your help.

I tried...

if ((dditems.Value == "Graduate")) {
dditems.Selected = true;
}

But it is telling me that it doesn't have a definition for '.Value' or '.Selected'

I've been looking through the intellisense list of options but can't find anything else.

Thanks
C
 
dditems is a collection not a specific item.
Code:
foreach (string toFind in mystrings[])
{
   foreach (ListItem li in ListBox1.Items) 
   {
      if (li.Value == toFind )
      {
         li.Selected = true;
      }
   }
}
also make sure the ListBox1.Mode = Multiple (not single)

Jason Meckley
Programmer
Specialty Bakers, Inc.
 
Hi All,
Thanks, I used similar to jasons code above and it still didn't work, but I think it was because I had the code in the onpageload event and the listbox in question was populated from a database table using a datasource in the HTML.
I think the above code was running before the listbox was populating so no matches were being made - so I populated the listbox in the onpageload event and it's working now.
Thanks for your help.
C
 
timing is everything:)

Jason Meckley
Programmer
Specialty Bakers, Inc.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top