Link to home
Start Free TrialLog in
Avatar of ptrennum
ptrennum

asked on

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
Avatar of martie_11
martie_11
Flag of United States of America image

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.
Avatar of Bob Learned
foreach (ListItem item in this.ListBox1.Items)
   if (item.Selected)
   {
   }

Bob
Avatar of ptrennum
ptrennum

ASKER

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>");
          }
        }
If you put a breakpoint on the if (tempListItem.Selected) line, does it reach that point?

Bob
it is not breaking into the:

if (tempListItem.Selected)
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
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?
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
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
ASKER CERTIFIED SOLUTION
Avatar of Bob Learned
Bob Learned
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
not checking for that - gonna try to figure out how to put it in now

thanks PT
Thanks Bob - that was the problem

PT