Populate a dynamic listbox's selected values with comma separated string or datareader
Posted on 2014-07-31
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
string items = strItems.Split(',');
foreach (string str in items)
txtState.SelectedValue = str;