C# 如何在Asp.net的CheckBoxList中获取选定的项目
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/8815963/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
how to get selected item in CheckBoxList in Asp.net
提问by Arian
I have a CheckBoxList in my page.is there any way to get all selected item values using linq?
我的页面中有一个 CheckBoxList。有没有办法使用 linq 获取所有选定的项目值?
what is the best way to get selected item values in CheckBoxList?
在 CheckBoxList 中获取所选项目值的最佳方法是什么?
采纳答案by A. Tapper
You could go about this by taking the items of the checkbox list and converting them to ListItems and from that collection fetch those who is selected, like this:
您可以通过获取复选框列表的项目并将它们转换为 ListItems 并从该集合中获取那些被选中的项目来解决这个问题,如下所示:
var selectedItems = yourCheckboxList.Items.Cast<ListItem>().Where(x => x.Selected);
回答by Laird Streak
Here's an easy way
这是一个简单的方法
foreach (System.Web.UI.WebControls.ListItem oItem in rdioListRoles.Items)
{
if (oItem.Selected) // if you want only selected
{
variable = oItem.Value;
}
// otherwise get for all items
variable = oItem.Value;
}
回答by Arif Ansari
List<string> selectedValues = chkBoxList1.Items.Cast<ListItem>().Where(li => li.Selected).Select(li => li.Value).ToList();

