Solved

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

Posted on 2014-07-31
8
406 Views
Last Modified: 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
            if (!string.IsNullOrEmpty(strItems))
            {
                string[] items = strItems.Split(',');

                foreach (string str in items)
                {
                    txtState.SelectedValue = str;
                }
            }
0
Comment
Question by:DAPayne
[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
  • 5
  • 3
8 Comments
 
LVL 23

Expert Comment

by:Jens Fiederer
ID: 40232223
I would expect you to get only the RIGHTMOST value to mind, since that would be last SelectedValue you set.
0
 
LVL 23

Expert Comment

by:Jens Fiederer
ID: 40232231
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
 

Author Comment

by:DAPayne
ID: 40232270
oops - that's what I meant... last, not first
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 23

Expert Comment

by:Jens Fiederer
ID: 40232335
OK, so you will want to go through txtState.Items and set the selected properties on the appropriate ones.
0
 

Author Comment

by:DAPayne
ID: 40232354
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
 
LVL 23

Accepted Solution

by:
Jens Fiederer earned 500 total points
ID: 40232408
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
 

Author Closing Comment

by:DAPayne
ID: 40232415
Thanks tons. I sometimes think in circles! :)
0
 
LVL 23

Expert Comment

by:Jens Fiederer
ID: 40232433
My pleasure....it seems that you were perfectly clear, but I WASN'T at first :-)
0

Featured Post

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
This video teaches viewers how to create their own website using cPanel and Wordpress. Tutorial walks users through how to set up their own domain name from tools like Domain Registrar, Hosting Account, and Wordpress. More specifically, the order in…
Use Wufoo, an online form creation tool, to make powerful forms. Learn how to selectively show certain fields based on user input using rules to gather relevant information and data from your forms. The rules feature provides you with an opportunity…

735 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