C# CheckBoxList绑定值,设置及获取
前端页面:.aspx
<asp:checkboxlist id="ckbdot" runat="server" repeatdirection="horizontal" style=" display: inline-block; vertical-align: middle;" cellpadding="10"></asp:checkboxlist>
后台处理:.aspx.cs
首先绑定值
例如:
using (datatable dt = new bllcccp_keywords().getkeywordsbysql(entityc2)) //checkboxlist 设置 ,用于绑定数据库中的数据
{
ckbdot.datasource = dt;
ckbdot.datatextfield = "word"//数据字段word
ckbdot.datavaluefield = "id"; //数据字段id 主键
ckbdot.databind();
}
获取选中的checkbox,用于插入数据库
string chkselect = "";
for (int i = 0; i < ckbdot.items.count; i++)
{
if (ckbdot.items[i].selected == true)
chkselect += ckbdot.items[i].text + ",";
}
编辑checkbox,用于从数据库中读取选中编辑
using (sqldatareader dr = new bllcccp_bank_activity().getbank_activitybyreader(entityc))
{
if (dr.read())
{
string _dot = dr["settlenode"].tostring();
string[] dot = _dot.split(',');
for (int i = 0; i < dot.length; i++)
{
for (int j = 0; j < ckbdot.items.count; j++)
{
if (dot[i] == ckbdot.items[j].text)
{
ckbdot.items[j].selected = true;
}
}
}
}
}
上一篇: 煮熟的鸡蛋能放几天,你知道吗