Consider this code:
foreach (ListItem item in lstViolations.Items)
{
if (item.Selected)
{
messageBody += item.Value + Environment.NewLine;
}
}
I am trying to iterate through each ListItem in lstViolations. However, only the first selected value is added to messageBody and I can’t figure out why that is.
Also, adding messageBody += "test" prints only the first list item followed by test.
Try this:
If it is a ListView, then:
If it is a Listbox, then:
This will iterate only through the selected items.
Edit: Didn’t looked at the tags. This will not work for ASP.NET !