前端頁面:.aspx <asp:CheckBoxList ID="ckbdot" runat="server" RepeatDirection="Horizontal" Style=" display: inline-block; vertical-align: middle;" CellPaddi ...
前端頁面:.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;
}
}
}
}
}