Solved

loop through multiple selection listbox

Posted on 2013-02-04
8
972 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
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!

 
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
 

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

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

ASP.Net to Oracle Connectivity Recently I had to develop an ASP.NET application connecting to an Oracle database.As I am doing it first time ,I had to solve several problems. This article will help to such developers  to develop an ASP.NET client…
Wouldn’t it be nice if you could test whether an element is contained in an array by using a Contains method just like the one available on List objects? Wouldn’t it be good if you could write code like this? (CODE) In .NET 3.5, this is possible…
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…
How to Install VMware Tools in Red Hat Enterprise Linux 6.4 (RHEL 6.4) Step-by-Step Tutorial

733 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