循环遍历复选框列表

2024-01-01

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

<asp:CheckBoxList ID="CheckBoxes" DataTextField="Value" DataValueField="Key" runat="server"></asp:CheckBoxList>

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

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

我收到错误

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


“事物”列表不包含 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));
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

循环遍历复选框列表 的相关文章

随机推荐