Solved

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

Posted on 2014-07-31
8
413 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
Why You Need a DevOps Toolchain

IT needs to deliver services with more agility and velocity. IT must roll out application features and innovations faster to keep up with customer demands, which is where a DevOps toolchain steps in. View the infographic to see why you need a DevOps toolchain.

 
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

How our DevOps Teams Maximize Uptime

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us. Read the use case whitepaper.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Stop Git from being my repository 1 49
Learning About The VB.NET TableLayoutPanel Control 5 42
PDF Turn Look 7 34
VB .net 2010 Byte array 2 21
Thoughout my experience working on eCommerce web applications I have seen applications succumbing to increased user demand and throughput. With increased loads the response times started to spike, which leads to user frustration and lost sales. I ha…
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.
This video teaches users how to migrate an existing Wordpress website to a new domain.
Learn how to set-up custom confirmation messages to users who complete your Wufoo form. Include inputs from fields in your form, webpage redirects, and more with Wufoo’s confirmation options.

738 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