Solved

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

Posted on 2014-07-31
8
367 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
 
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
Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

 

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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
.Net application crashing 6 21
Code enhancement 5 13
cookies analysis tools 2 29
Close word object 13 24
Developer tools in browsers have been around for a while, yet they are still heavily underused by developers. Developers still fix html or CSS then refresh page to see effect, or they put alert or debugger in JavaScript and then try again and again …
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
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.
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…

746 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

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now