Populate a dynamic listbox's selected values with comma separated string or datareader

I have a dynamic listbox containing US states that is being populated On page load. What I want to do is then determine selected values based on a string read by a datareader. For example, the string might say CA, UT, NV, WA, and I want it to split that string (based on the comma) and then use each value to populate the selected values of that listbox. So far, I can only get the leftmost value to bind. Any idea what's wrong with my code???

            string strItems = dbReader["STATES"].ToString();  // THIS IS THE DATA READ FROM THE TABLE
            if (!string.IsNullOrEmpty(strItems))
            {
                string[] items = strItems.Split(',');

                foreach (string str in items)
                {
                    txtState.SelectedValue = str;
                }
            }
DAPayneAsked:
Who is Participating?
 
Jens FiedererConnect With a Mentor Test Developer/ValidatorCommented:
That was actually an ANSWER rather than a question :-)

I meant:

 string strItems = dbReader["STATES"].ToString();  // THIS IS THE DATA READ FROM THE TABLE
            if (!string.IsNullOrEmpty(strItems))
            {
                string[] items = strItems.Split(',');

                txtState.SelectionMode = ListSelectionMode.Multiple;
                foreach (ListItem item in txtState.Items)
               {
                      item.Selected = items.Contains(item.Value);
                }

            }

Open in new window

0
 
Jens FiedererTest Developer/ValidatorCommented:
I would expect you to get only the RIGHTMOST value to mind, since that would be last SelectedValue you set.
0
 
Jens FiedererTest Developer/ValidatorCommented:
Anyway, for doing multiple selections you want to set the Selected flag on the individual items in the Items collection.

The SelectedValue is only good for setting a single value, and (on reading it) only returns the first of the selected values.
0
The new generation of project management tools

With monday.com’s project management tool, you can see what everyone on your team is working in a single glance. Its intuitive dashboards are customizable, so you can create systems that work for you.

 
DAPayneAuthor Commented:
oops - that's what I meant... last, not first
0
 
Jens FiedererTest Developer/ValidatorCommented:
OK, so you will want to go through txtState.Items and set the selected properties on the appropriate ones.
0
 
DAPayneAuthor Commented:
Correct - the listbox contains each of the US states and territories (loaded on page_load from a sql table)

Another table has customer data. The field I'm interested in is 'States_served'. The datareader value will have comma separated state values (DC, MD, VA for example)

And, I want each value to show as selected in the listbox.

Sorry if I wasn't that clear in the beginning.
0
 
DAPayneAuthor Commented:
Thanks tons. I sometimes think in circles! :)
0
 
Jens FiedererTest Developer/ValidatorCommented:
My pleasure....it seems that you were perfectly clear, but I WASN'T at first :-)
0
All Courses

From novice to tech pro — start learning today.