Solved

loop through multiple selection listbox

Posted on 2013-02-04
8
968 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
Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

 
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

Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

Question has a verified solution.

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

Suggested Solutions

Just a quick little trick I learned recently.  Now that I'm using jQuery with abandon in my asp.net applications, I have grown tired of the following syntax:      (CODE) I suppose it just offends my sense of decency to put inline VBScript on a…
For those of you who don't follow the news, or just happen to live under rocks, Microsoft Research released a beta SDK (http://www.microsoft.com/en-us/download/details.aspx?id=27876) for the Xbox 360 Kinect. If you don't know what a Kinect is (http:…
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…

828 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