当前位置: 代码迷 >> ASP.NET >> 怎么获取checkboxlist中选中或者没选中的值
  详细解决方案

怎么获取checkboxlist中选中或者没选中的值

热度:5012   发布时间:2013-02-25 00:00:00.0
如何获取checkboxlist中选中或者没选中的值
现在我有个checkboxlist,如何获取checkboxlist中选中或者没选中的值.
得到状态.checkboxlist的选项是从数据库里取到的.

------解决方案--------------------------------------------------------
for (int i = 0; i < cbl.Items.Count; i++)
{
if (cbl.Items[i].Selected)
{
//这里是选中的项
}
}
------解决方案--------------------------------------------------------
/// <summary>
/// 设置CheckBoxList默认值
/// </summary>
/// <param name= "cbl "> CheckBoxList名称(以分隔符[参数:separator]分割的字符串,比如:1,2,3,4...或a,b,c,d...) </param>
/// <param name= "defaultValue "> 默认值 </param>
/// <param name= "type "> 默认匹配类型 </param>
/// <param name= "separator "> 分隔符 </param>
public static void SetCheckBoxListDefaultValue(CheckBoxList cbl, string defaultValue, FindType type, char separator)
{
string[] arrValue = defaultValue.Split(separator);

for (int j = 0; j < arrValue.Length; j++)
{
for (int i = 0; i < cbl.Items.Count; i++)
{
if (type == FindType.Value)
{
if (cbl.Items[i].Value == arrValue[j].ToString())
{
cbl.Items[i].Selected = true;
break;
}
}
else if (type == FindType.Text)
{
if (cbl.Items[i].Text == arrValue[j].ToString())
{
cbl.Items[i].Selected = true;
break;
}
}
}
}
}