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
Thanks,
PT
foreach (ListItem item in this.ListBox1.Items)
if (item.Selected)
{
}
Bob
if (item.Selected)
{
}
Bob
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(tempListIte m.Value);
Response.Write("peter<br>" );
}
}
foreach (ListItem tempListItem in lbHunters.Items)
{
if (tempListItem.Selected)
{
Response.Write(tempListIte
Response.Write("peter<br>"
}
}
If you put a breakpoint on the if (tempListItem.Selected) line, does it reach that point?
Bob
Bob
ASKER
it is not breaking into the:
if (tempListItem.Selected)
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
My guess is that you don't have EnableViewState = true, and you've lost all the items in the list.
Bob
ASKER
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?
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
Bob
ASKER
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
lbHunters.DataSource = objRdr;
lbHunters.DataValueField = "BugHunterID";
lbHunters.DataTextField = "Handle";
lbHunters.DataBind();
thats the only difference
PT
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
not checking for that - gonna try to figure out how to put it in now
thanks PT
thanks PT
ASKER
Thanks Bob - that was the problem
PT
PT
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(strin
{
reasonCodes += ",";
reasonCodesID += ",";
}
reasonCodes += tempListItem.Text;
reasonCodesID += tempListItem.Value.ToStrin
}
}
This way you get all the items in one string separated by commas...of course you can replace:
...
if (!reasonCodes.Equals(strin
{
reasonCodes += ",";
reasonCodesID += ",";
}
reasonCodes += tempListItem.Text;
reasonCodesID += tempListItem.Value.ToStrin
...
with whatever you want...
...hope that helps.