我创建了Checkbox list和Button,使用C#在ASP.Net应用程序中存储Checked Values,如下所示,
<asp:CheckBoxList ID="CheckBoxList1" runat="server" Width="120px">
</asp:CheckBoxList>
<asp:Button ID="AddCheckedData" Height="25px" runat="server" Text="Add Checked Data"
onclick="AddCheckedData_Click"/>
我已将Code中的CheckBoxList绑定为,
public void bindchecklist()
{
B.SFS_CODE = Convert.ToInt32(TxtHQCode.Text);
B.id = 7; //tblparent AND tblchild
ds8 = A.DATA8(B);
CheckBoxList1.DataSource = ds8.Tables[0];
CheckBoxList1.DataTextField = "EMP_NAME";
CheckBoxList1.DataValueField = "EMP_CODE";
CheckBoxList1.DataBind();
}
按钮单击事件将所选值存储在DataBase中,如下所示,
string selemployee = string.Empty;
foreach (ListItem item in CheckBoxList1.Items)
{
if (item.Selected)
{
selemployee = item.Value;
B.id = 8;//insert into DCR_CHECKED
B.Emp_Code = selemployee;
ds8 = A.DATA8(B);
}
}
现在,我有另一个按钮来编辑CheckBoxList Selection.Now,我想从数据库中检索数据,并在单击编辑按钮时检查数据库中可用的复选框(在单击保存按钮时在数据库中插入复选框值)。
答案 0 :(得分:1)
请尝试:
for each (DataRow row in resultTable.Rows)
{
if (Convert.ToBoolean(row["bit_column_name"]))
{
// check box is "1" (checked) in the database
.....
}
}
答案 1 :(得分:0)
foreach (DataTable table in ds8.Tables)
{
foreach (DataRow dr in table.Rows)
{
String S = Convert.ToString(dr["EMP_CODE"].ToString());
foreach (ListItem item in CheckBoxList2.Items)
{
if (S == item.Value)
{
item.Selected = true;
}
}
}
}