从c#中的CheckBoxList获取未经检查的值

时间:2014-12-12 06:29:28

标签: c# asp.net

我正在尝试在button_click中更新CheckBoxList中未经检查的值。 并且无法获取CheckBoxList未选中项目的值。

我的填充chechboxlist代码是

 using (SqlConnection conn = new SqlConnection())
    {
        conn.ConnectionString = ConfigurationManager
                .ConnectionStrings["constr"].ConnectionString;
        using (SqlCommand cmd = new SqlCommand())
        {
            cmd.CommandText = "select * from hobbies";
            cmd.Connection = conn;
            conn.Open();
            using (SqlDataReader sdr = cmd.ExecuteReader())
            {
                while (sdr.Read())
                {
                    ListItem item = new ListItem();
                    item.Text = sdr["Hobby"].ToString();
                    item.Value = sdr["HobbyId"].ToString();
                    item.Selected = Convert.ToBoolean(sdr["IsSelected"]);
                    chkHobbies.Items.Add(item);
                }
            }
           conn.Close();
        }
    }

我正在使用答案https://stackoverflow.com/a/410505/2376607

但它适用于Windows窗体

请帮助您获取CheckBoxList未经检查的值。

2 个答案:

答案 0 :(得分:3)

您可以尝试下面的代码示例: -

        string chkboxlistValue = "";
        string uncheckedId = "";
        foreach (ListItem val in chkbxId.Items)
        {
            if (val.Selected)
            {
                chkboxlistValue += val.Value + " ";
            }
            else
            {
                 uncheckedId += val.Value + ",";
            }
        }

答案 1 :(得分:1)

foreach (ListItem item in chkHobbies.Items)
{
   if (item.Selected == false)
   {
      // your code here
   }
}