简体   繁体   English

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

[英]Get unchecked values from CheckBoxList in c#

I am trying to update the unchecked values from CheckBoxList in button_click. 我正在尝试从button_click中的CheckBoxList更新未检查的值。 and not able to get the values of CheckBoxList unchecked items. 并且无法获取CheckBoxList未选中项的值。

my code for populate chechboxlist is 我用于填充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();
        }
    }

i am using the answer https://stackoverflow.com/a/410505/2376607 我正在使用答案https://stackoverflow.com/a/410505/2376607

but it is for windows forms 但这是针对Windows窗体的

please help how to get the unchecked values of CheckBoxList . 请帮助如何获取CheckBoxList的未经检查的值。

You can try below sample of code :- 您可以尝试以下代码示例:-

        string chkboxlistValue = "";
        string uncheckedId = "";
        foreach (ListItem val in chkbxId.Items)
        {
            if (val.Selected)
            {
                chkboxlistValue += val.Value + " ";
            }
            else
            {
                 uncheckedId += val.Value + ",";
            }
        }
foreach (ListItem item in chkHobbies.Items)
{
   if (item.Selected == false)
   {
      // your code here
   }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM