循环遍历复选框列表
我正在构建一个复选框列表:
<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)' 有一些无效参数“
I am building a checkbox lists:
<asp:CheckBoxList ID="CheckBoxes" DataTextField="Value" DataValueField="Key" runat="server"></asp:CheckBoxList>
And trying to get the value's of the selected items:
List<Guid> things = new List<Guid>();
foreach (ListItem item in this.CheckBoxes.Items)
{
if (item.Selected)
things.Add(item.Value);
}
}
I get the errror
"The best overloaded method match for
'System.Collections.Generic.List.Add(System.Guid)'
has some invalid arguments "
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
“事物”列表不包含 Guid 值。您应该将 item.value 转换为 Guid 值:
The 'thing' list is excepting a Guid value. You should convert item.value to a Guid value:
ListItem.Value
的类型为System.String
,并且您尝试将其添加到List
中。您可以尝试:只要字符串值可解析为
Guid
就可以了。如果不清楚,您需要更加小心并使用Guid.TryParse(item.Value)
。ListItem.Value
is of typeSystem.String
, and you're trying to add it to aList<Guid>
. You could try:That will work as long as the string value is parsable to a
Guid
. If that's not clear, you'll want to be more careful and useGuid.TryParse(item.Value)
.如果您的列表的 Add 方法确实接受 GUID(请参阅错误消息),但不接受“item.value”,那么我猜 item.value 不是 GUID。
试试这个:
If your List's Add method does accept GUID's (see the error message), but isn't accepting "item.value", then I'd guess item.value is not a GUID.
Try this: