Reputation: 1691
I'm trying to get the selected items from a checkboxlist and add them to a listbox on button click. I was able to achieve this by using the following:
protected void AddSelectedField(object sender, EventArgs e)
{
foreach (ListItem column in ColumnsList.Items)
{
if (column.Selected)
{
SelectedColumns.Add(column.Text);
}
}
foreach (String column in SelectedColumns)
{
SelectedFieldsList.Items.Add(column);
}
}
However, the problem with this is that the user can still add the same item that they've already selected after the postback occurs. I've tried the following but this doesn't even add the selected items to the listbox anymore:
protected void AddSelectedField(object sender, EventArgs e)
{
foreach (ListItem column in ColumnsList.Items)
{
if (column.Selected)
{
foreach (String item in SelectedColumns)
{
if (item != column.Text || SelectedColumns != null)
{
SelectedColumns.Add(column.Text);
}
}
}
}
foreach (String column in SelectedColumns)
{
SelectedFieldsList.Items.Add(column);
}
}
Alternatively, I also need to add functionality that allows the user to remove items from the list by looping through the selected items in the listbox. I've tried this:
protected void RemoveSelectedField(object sender, EventArgs e)
{
foreach (ListItem field in SelectedFieldsList.Items)
{
if (field.Selected)
{
SelectedColumns.Remove(item);
}
}
foreach (String field in SelectedColumns)
{
SelectedFieldsList.Items.Add(field);
}
}
As well as:
protected void RemoveSelectedField(object sender, EventArgs e)
{
foreach (ListItem field in SelectedFieldsList.Items)
{
if (field.Selected)
{
foreach (String item in SelectedColumns)
{
if (item == field.Text)
{
SelectedColumns.Remove(item);
}
}
}
}
foreach (String field in SelectedColumns)
{
SelectedFieldsList.Items.Add(field);
}
}
Neither of these removed the selected items from the listbox m(and I'm assuming the string list as well). In regards to checking to see if items that are being added already exist I've also tried:
protected void AddSelectedField(object sender, EventArgs e)
{
foreach (ListItem column in ColumnsList.Items)
{
if (column.Selected && !SelectedColumns.Contains(column.Text))
{
SelectedColumns.Add(column.Text);
}
}
foreach (String column in SelectedColumns)
{
SelectedFieldsList.Items.Add(column);
}
}
And it's as if it ignores the second part of the if statement that checks to see if the list contains the current list item's text.
Upvotes: 3
Views: 2705
Reputation: 1928
The new functions have a big no-no...
You cannot iterate through a list, while also changing that list:
foreach (String item in SelectedColumns)
{
if (item == field.Text)
{
SelectedColumns.Remove(item);
}
}
and...
foreach (String item in SelectedColumns)
{
if (item != column.Text || SelectedColumns != null)
SelectedColumns.Add(item.text);
}
Doing this should cause an exception which makes me think that code is not even being called.
Also, for the second code snippit, I think that SelectedColumns != null is probably not what you meant, since if it was null you would not be iterating through it.
Finally, you might use var instead of String/ListItem and debug to see what types of objects you are actually iterating through.
Upvotes: 0
Reputation: 402
If SelectedColumns is a List why don't use Contains() method?
if (column.Selected && !SelectedColumns.Contains(column.Text))
{
SelectedColumns.Add(column.Text);
}
Upvotes: 2