您的位置:首页 > 其它

CheckBoxList去分号绑定选中值

2015-08-20 16:51 281 查看
数据库表中的样式:

人;狗;猫;( 数据库对应字段名Text)

对应的value:1;2;3; (数据库中的字段名:itemValue)

前段checkboxlist的ID:checkbox

//初始化页面给CheckBoxList选中的复选框 赋值

for (int i = 0; i < itemValue.Value.Split(';').Length; i++)

{

for (int j = 0; j < checkbox.Items.Count; j++)

{

if (itemValue.Value.Split(';')[i] == CHB_FenLei.Items[j].Value)

{

checkbox.Items[j].Selected = true;

}

}

//复选框拼接后存入数据库为一个字段

string itemvalue="";

string text="";

for (int i = 0; i <
checkbox.Items.Count; i++)

{

if (checkbox.Items[i].Selected)

{

itemvalue+=
checkbox.Items[i].Value + ";";

text+= checkbox.Items[i].Text + ";";

}

}

或者: string m_strTemp = string.Empty;

for (int i = 0; i < CBoxListAnswer.Items.Count; i++)//读取CheckBoxList
选中的值,保存起来

{

if (CBoxListAnswer.Items[i].Selected)

{

m_strTemp += CBoxListAnswer.Items[i].Value + ",";

}

}

if (!string.IsNullOrEmpty(m_strTemp))

Label1.Text = m_strTemp.Substring(0, m_strTemp.Length - 1);

else

Label1.Text = m_strTemp;
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: