I am having difficulties in the below question
How do I create the below ListItem
method to return a array of string
s?
public static List<ListItem> GetSelectedListItems(CheckBoxList _ddl)
{
List<ListItem> GetData = new List<ListItem>();
foreach (ListItem item in _ddl.Items)
{
if (item.Selected) GetData.Add(item);
}
return GetData;
}
Try
public static string[] GetSelectedListItems(CheckBoxList _ddl)
{
List<string> GetData = new List<string>();
foreach (ListItem item in _ddl.Items)
{
if (item.Selected) GetData.Add(item.Text);
}
return GetData.ToArray();
}
You have to use Enumerable.Cast
because CheckBoxList.Items
isn't a generic collection:
return _ddl.Items.Cast<ListItem>().Where(i => i.Selected).ToList();
If you want to return string[]
of all selected items:
return _ddl.Items.Cast<ListItem>().Where(i => i.Selected).Select(i => i.Text).ToArray();
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.