How to Retrieve multiple selections from a list box?

I have a multi line list box and I need to get the values selected by the user when the form is submitted.  I am using asp.net and c#.

Thanks,

PT
ptrennumAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

martie_11Commented:
Hello,

In the past I have used the following:

      string reasonCodes = string.Empty;
      string reasonCodesID = string.Empty;
      foreach (ListItem tempListItem in lstReasonCodes.Items)
      {
            if (tempListItem.Selected)
            {
                  if (!reasonCodes.Equals(string.Empty))
                  {
                        reasonCodes      += ",";
                        reasonCodesID      += ",";
                  }
                  reasonCodes      += tempListItem.Text;
                  reasonCodesID      += tempListItem.Value.ToString();
            }
      }

This way you get all the items in one string separated by commas...of course you can replace:

                  ...
                  if (!reasonCodes.Equals(string.Empty))
                  {
                        reasonCodes      += ",";
                        reasonCodesID      += ",";
                  }
                  reasonCodes      += tempListItem.Text;
                  reasonCodesID      += tempListItem.Value.ToString();
                  ...

with whatever you want...

...hope that helps.
Bob LearnedCommented:
foreach (ListItem item in this.ListBox1.Items)
   if (item.Selected)
   {
   }

Bob
ptrennumAuthor Commented:
I like your method Bob however with this code in place it does not output anything to my screen as if it didn't even hit the loop.

foreach (ListItem tempListItem in lbHunters.Items)
        {
          if (tempListItem.Selected)
          {
               Response.Write(tempListItem.Value);
               Response.Write("peter<br>");
          }
        }
Introduction to Web Design

Develop a strong foundation and understanding of web design by learning HTML, CSS, and additional tools to help you develop your own website.

Bob LearnedCommented:
If you put a breakpoint on the if (tempListItem.Selected) line, does it reach that point?

Bob
ptrennumAuthor Commented:
it is not breaking into the:

if (tempListItem.Selected)
Bob LearnedCommented:
What does lbHunters.Items.Count equal, at the top of the loop at foreach (ListItem tempListItem in lbHunters.Items)?

My guess is that you don't have EnableViewState = true, and you've lost all the items in the list.

Bob
ptrennumAuthor Commented:
EnableViewState is equal to true.

The count is correct - I have been printing response writes to show the count.  

the items are still in the list it is just not recognizing that there are any items that are selected

is the tempListItem automatically assigned the value from lbHunters.Items?
Bob LearnedCommented:
I ran a small, simple test, manually adding items to a list, and selecting multiple items, and posting back, and each time I can retrieve the values of Item.Selected correctly, so there appears to be something in this situation that is different.

Bob
ptrennumAuthor Commented:
The listbox is being binded and populated with a query on the Page_load event

lbHunters.DataSource = objRdr;
        lbHunters.DataValueField = "BugHunterID";
        lbHunters.DataTextField = "Handle";
        lbHunters.DataBind();

thats the only difference

PT
Bob LearnedCommented:
Are you checking for Page.IsPostBack?  You only need to bind the data when Page.IsPostBack = false.  After submit, Page.IsPostBack is true, and then you are re-binding the ListBox, and starting over again, which clears all your selections.

Bob

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
ptrennumAuthor Commented:
not checking for that - gonna try to figure out how to put it in now

thanks PT
ptrennumAuthor Commented:
Thanks Bob - that was the problem

PT
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C#

From novice to tech pro — start learning today.