David Thor
David Thor

Reputation: 13

Getting all values from Checkboxlist

I have a array of 13 strings in which I pull the string values from checkboxlist. I can pull the values that are selected with the code below. However I want to also pull the unchecked values as null. So if I select 12 values then 1 would be null in the array.

I'm not sure if the array is dynamically adding the selected values or if the code fills the uncheck values with null. Please help.Thank you.

string[] selectedAreaValues = new string[13];

IEnumerable<string> allChecked = (from item in ceCheckBoxList.Items.Cast<ListItem>()
                                  where item.Selected
                                  select item.Value);

selectedAreaValues = allChecked.ToArray();

Upvotes: 1

Views: 233

Answers (2)

alainlompo
alainlompo

Reputation: 4434

We may also use the alternative syntaxe + A concat between the two sequences

  1. IEnumerable selectedItems = ceCheckBoxList.Items.Cast().Where(item => item.Selected).Select(item => new String(item.Value.toCharArray()));

  2. Using a negation with the item.Selected boolean expression in the Where clause and using a Concat between the two sequences.

Upvotes: 0

Peter Duniho
Peter Duniho

Reputation: 70652

If you want to always return the same number of items as in the original Items collection, but project selected items to their value and unselected items to null, then something like this should work:

IEnumerable<string> allChecked = (from item in ceCheckBoxList.Items.Cast<ListItem>()
                                  select item.Selected ? item.Value : (string)null);

Upvotes: 1

Related Questions