簡體   English   中英

如何在Asp.net中的CheckBoxList中獲取所選項目

[英]how to get selected item in CheckBoxList in Asp.net

我的頁面中有一個CheckBoxList。有沒有辦法使用linq獲取所有選定的項目值?

在CheckBoxList中獲取所選項值的最佳方法是什么?

您可以通過獲取復選框列表中的項目並將它們轉換為ListItems並從該集合中獲取所選內容,如下所示:

var selectedItems = yourCheckboxList.Items.Cast<ListItem>().Where(x => x.Selected);

這是一個簡單的方法

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;
}
List<string> selectedValues = chkBoxList1.Items.Cast<ListItem>().Where(li => li.Selected).Select(li => li.Value).ToList();

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM