【问题标题】:How to check checkbox based on database values in C#如何根据 C# 中的数据库值检查复选框
【发布时间】:2017-07-11 00:41:15
【问题描述】:

我已经创建了复选框列表和按钮来使用 C# 在 ASP.Net 应用程序中存储检查值,如下所示,

 <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"/>

我已将 CheckBoxList 绑定在 Code behind as,

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 选择。现在,我想从数据库中检索数据并在单击编辑按钮时检查数据库中可用的复选框(单击保存按钮时在数据库中插入的复选框值)。

【问题讨论】:

    标签: c# asp.net checkbox checkboxlist


    【解决方案1】:

    请尝试:

    for each (DataRow row in resultTable.Rows)
    {
        if (Convert.ToBoolean(row["bit_column_name"]))
        {
             // check box is "1" (checked) in the database
             .....
        }
    }
    

    【讨论】:

    • 感谢您的回复。在您的回复的帮助下,我自己找到了答案。
    【解决方案2】:
    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;
                                }
                            }
                        }
                    }
    

    【讨论】:

      猜你喜欢
      • 2013-03-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-01-09
      • 1970-01-01
      • 2012-08-31
      • 2020-08-23
      相关资源
      最近更新 更多