Solved

loop through multiple selection listbox

Posted on 2013-02-04
8
974 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
[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
  • 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
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.

 
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

Instantly Create Instructional Tutorials

Contextual Guidance at the moment of need helps your employees adopt to new software or processes instantly. Boost knowledge retention and employee engagement step-by-step with one easy solution.

Question has a verified solution.

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

IntroductionWhile developing web applications, a single page might contain many regions and each region might contain many number of controls with the capability to perform  postback. Many times you might need to perform some action on an ASP.NET po…
The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
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…
In this video we outline the Physical Segments view of NetCrunch network monitor. By following this brief how-to video, you will be able to learn how NetCrunch visualizes your network, how granular is the information collected, as well as where to f…

729 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