数据库表中的样式:
人;狗;猫;( 数据库对应字段名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;
本文详细阐述了如何在数据库中定义和操作不同类型的记录,包括人、狗、猫等,并通过前端的checkboxlist实现数据的选择和展示。进一步介绍了如何将前端选择的数据拼接并存入数据库,以及数据读取过程中的注意事项。
1401

被折叠的 条评论
为什么被折叠?



