Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

loop through multiple selection listbox

Posted on 2013-02-04
8
Medium Priority
?
991 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 800 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
What is SQL Server and how does it work?

The purpose of this paper is to provide you background on SQL Server. It’s your self-study guide for learning fundamentals. It includes both the history of SQL and its technical basics. Concepts and definitions will form the solid foundation of your future DBA expertise.

 
LVL 42

Assisted Solution

by:Meir Rivkin
Meir Rivkin earned 400 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:Meir Rivkin
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

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

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

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…
Simulator games are perfect for generating sample realistic data streams, especially for learning data analysis. It is even useful for demoing offerings such as Azure stream analytics, PowerBI etc.
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…
Is your data getting by on basic protection measures? In today’s climate of debilitating malware and ransomware—like WannaCry—that may not be enough. You need to establish more than basics, like a recovery plan that protects both data and endpoints.…

971 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