troubleshooting Question

how do I set selected items in a dynamic listbox with a string array

Avatar of megatherian
megatherianFlag for United States of America asked on
.NET ProgrammingASP.NET
4 Comments1 Solution774 ViewsLast Modified:
I have a listbox that's values populate from a SQL datasource.  In the form on the page there is also a drop down list and when it's value changes the items in the listbox change accordingly (well, technically, it changes a sql parameter that updates the listbox).

In another SQL query I have a list of values separated by commas.  I then turn that into a string array.  The values in that string array correspond to values in the listbox I'd like selected.  This is where I'm running into trouble - how do I get the listbox items to be selected?  I believe the trouble I'm running into is when to run the proceedure, I've tried in the page load, databound, text changed, selecting - I can't seem to find where to put it so that it checks after the listbox is updated with the SQL info.
// SelectedItem is my string array
 
for (int i = 0; i < SelectedItem.Length; i++)
            {
                myListbox.Items.FindByValue(SelectedItem[i].ToString()).Selected = true;
            }
Join the community to see this answer!
Join our exclusive community to see this answer & millions of others.
Unlock 1 Answer and 4 Comments.
Join the Community
Learn from the best

Network and collaborate with thousands of CTOs, CISOs, and IT Pros rooting for you and your success.

Andrew Hancock - VMware vExpert
See if this solution works for you by signing up for a 7 day free trial.
Unlock 1 Answer and 4 Comments.
Try for 7 days

”The time we save is the biggest benefit of E-E to our team. What could take multiple guys 2 hours or more each to find is accessed in around 15 minutes on Experts Exchange.

-Mike Kapnisakis, Warner Bros