Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

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

Posted on 2014-07-31
8
Medium Priority
?
463 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
  • 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
Veeam Disaster Recovery in Microsoft Azure

Veeam PN for Microsoft Azure is a FREE solution designed to simplify and automate the setup of a DR site in Microsoft Azure using lightweight software-defined networking. It reduces the complexity of VPN deployments and is designed for businesses of ALL sizes.

 
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 2000 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

Technology Partners: 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!

Question has a verified solution.

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

A long time ago (May 2011), I have written an article showing you how to create a DLL using Visual Studio 2005 to be hosted in SQL Server 2005. That was valid at that time and it is still valid if you are still using these versions. You can still re…
These days, all we hear about hacktivists took down so and so websites and retrieved thousands of user’s data. One of the techniques to get unauthorized access to database is by performing SQL injection. This article is quite lengthy which gives bas…
This video teaches users how to migrate an existing Wordpress website to a new domain.
Wufoo.com provides powerful tools for surveying targeted groups, and utilizing data from completed surveys to find trends, discover areas of demand or customer expectation, and make business decisions on products or services.
Suggested Courses

824 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