循环显示复选框列表

6 asp.net foreach checkboxlist

我正在构建一个复选框列表:

<asp:CheckBoxList ID="CheckBoxes" DataTextField="Value" DataValueField="Key" runat="server"></asp:CheckBoxList>
Run Code Online (Sandbox Code Playgroud)

并尝试获取所选项目的值:

List<Guid> things = new List<Guid>();
foreach (ListItem item in this.CheckBoxes.Items)
{
    if (item.Selected)
        things.Add(item.Value);
    }
}
Run Code Online (Sandbox Code Playgroud)

我得到了错误

"'System.Collections.Generic.List.Add(System.Guid)'的最佳重载方法匹配'有一些无效的参数"

jde*_*per 8

"事物"列表不包括Guid值.您应该将item.value转换为Guid值:

List<Guid> things = new List<Guid>();
foreach (ListItem item in this.CheckBoxes.Items)
{
  if (item.Selected)
    things.Add(new Guid(item.Value));
}
Run Code Online (Sandbox Code Playgroud)