欢迎来到代码驿站!

.NET代码

当前位置:首页 > 软件编程 > .NET代码

C#遍历得到checkboxlist选中值和设置选中项的代码

时间:2021-05-04 10:51:28|栏目:.NET代码|点击:

得到选中项的value值并拼接成一个字符串返回

public string GetChecked(CheckBoxList checkList, string separator)
{
string str = "";
for (int i = 0; i < checkList.Items.Count; i++)
{
if (checkList.Items[i].Selected)
{
str += checkList.Items[i].Value + separator;
}
}
return str;
}

有选中字符串 分割之后遍历选中对应value值得选项

public void SetChecked(CheckBoxList checkList, string selval, string separator)
{
selval = separator + selval + separator; //例如:"0,1,1,2,1"->",0,1,1,2,1,"
for (int i = 0; i < checkList.Items.Count; i++)
{
checkList.Items[i].Selected = false;
string val = separator + checkList.Items[i].Value + separator;
if (selval.IndexOf(val) != -1)
{
checkList.Items[i].Selected = true;
selval = selval.Replace(val, separator); //然后从原来的值串中删除已经选中了的
if (selval == separator) //selval的最后一项也被选中的话,此时经过Replace后,只会剩下一个分隔符
{
selval += separator; //添加一个分隔符
}
}
}
}

上一篇:ASP.Net不执行问题一解

栏    目:.NET代码

下一篇:可替代log4j日志的c#简单日志类队列实现类代码分享

本文标题:C#遍历得到checkboxlist选中值和设置选中项的代码

本文地址:http://www.codeinn.net/misctech/114774.html

推荐教程

广告投放 | 联系我们 | 版权申明

重要申明:本站所有的文章、图片、评论等,均由网友发表或上传并维护或收集自网络,属个人行为,与本站立场无关。

如果侵犯了您的权利,请与我们联系,我们将在24小时内进行处理、任何非本站因素导致的法律后果,本站均不负任何责任。

联系QQ:914707363 | 邮箱:codeinn#126.com(#换成@)

Copyright © 2020 代码驿站 版权所有