Solved

loop through multiple selection listbox

Posted on 2013-02-04
8
952 Views
Last Modified: 2013-02-04
I need to loop through a multiple selection listbox and retrieve selected values.

Three rows were selected. The following code goes to the first selected valuethree times.
 
 string strItems = "";
        foreach (ListItem row in lstKeyword.Items)  
        {
            if (row.Selected == true)
            {
                strItems = strItems + int.Parse(lstKeyword.SelectedValue.ToString()) + ",";
            }
        }

//strItems returns 1267,1267,1267,

How can I move to the next selected value?

Thanks,
0
Comment
Question by:Dovberman
  • 4
  • 2
  • 2
8 Comments
 
LVL 25

Accepted Solution

by:
Rouchie earned 200 total points
ID: 38850539
string strItems = "";
        foreach (ListItem row in lstKeyword.Items)  
        {
            if (row.Selected == true)
            {
                strItems = strItems + int.Parse(row.Value) + ",";
            }
        }
0
 
LVL 25

Expert Comment

by:Rouchie
ID: 38850545
ListControl.SelectedValue.ToString() always returns the first selected value, even if multiple items are selected.

So... you have to cast each one as a ListItem (like you have done) then refer to that respective item's Value property.
0
 
LVL 25

Expert Comment

by:Rouchie
ID: 38850557
Actually this is better because it avoids parsing to int then back to string, so less code to achieve the same result...

string strItems = "";
        foreach (ListItem row in lstKeyword.Items)  
        {
            if (row.Selected == true)
            {
                strItems = strItems + row.Value + ",";
            }
        } 

Open in new window

0
 
LVL 42

Assisted Solution

by:sedgwick
sedgwick earned 100 total points
ID: 38850573
string strItems = String.Join(",", lstKeyword.Items.Cast<ListItem>().Where(n=>n.Selected).Select(n=>n.Value).ToArray());

Open in new window

0
What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

 

Author Comment

by:Dovberman
ID: 38850607
So how do I go to the next selected item?
0
 
LVL 42

Expert Comment

by:sedgwick
ID: 38850613
This code returns all selected values concatenated in a single string
and separated with comma
0
 

Author Comment

by:Dovberman
ID: 38850618
Got it.

      string strItems = "";
        foreach (ListItem row in lstKeyword.Items)  
        {
            if (row.Selected == true)
            {
                strItems = strItems + int.Parse(row.Value) + ",";
            }
         }

Thanks
0
 
LVL 25

Expert Comment

by:Rouchie
ID: 38850619
That's what I posted (See above about the Int.Parse bit which you don't need)!
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

More often than not, we developers are confronted with a need: a need to make some kind of magic happen via code. Whether it is for a client, for the boss, or for our own personal projects, the need must be satisfied. Most of the time, the Framework…
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
This is Part 3 in a 3-part series on Experts Exchange to discuss error handling in VBA code written for Excel. Part 1 of this series discussed basic error handling code using VBA. http://www.experts-exchange.com/videos/1478/Excel-Error-Handlin…
You have products, that come in variants and want to set different prices for them? Watch this micro tutorial that describes how to configure prices for Magento super attributes. Assigning simple products to configurable: We assigned simple products…

762 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

20 Experts available now in Live!

Get 1:1 Help Now