通过复选框列表循环 [英] Loop through a checkbox list

查看:220
本文介绍了通过复选框列表循环的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我建立一个复选框列表:

 < ASP:的CheckBoxList ID =的CheckBoxDataTextField =值DataValueField =密钥=服务器>< / ASP:的CheckBoxList>

和试图获得该值的选定项目:

 列表<&的Guid GT;事情=新的List<&的Guid GT;();
的foreach(在this.CheckBoxes.Items列表项的项目)
{
    如果(item.Selected)
        things.Add(item.Value);
    }
}

我得到的errror


  

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



解决方案

在'东西'名单除外一个GUID值。你应该item.value转换为一个GUID值:

 列表<&的Guid GT;事情=新的List<&的Guid GT;();
的foreach(在this.CheckBoxes.Items列表项的项目)
{
  如果(item.Selected)
    things.Add(新的GUID(item.Value));
}

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 "

解决方案

The 'thing' list is excepting a Guid value. You should convert item.value to a Guid value:

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

这篇关于通过复选框列表循环的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆