-1

I am trying to update the unchecked values from CheckBoxList in button_click. and not able to get the values of CheckBoxList unchecked items.

my code for populate chechboxlist is

 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

but it is for windows forms

please help how to get the unchecked values of CheckBoxList.

Community
  • 1
  • 1
Gitz
  • 810
  • 1
  • 17
  • 48

2 Answers2

3

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 + ",";
            }
        }
Neel
  • 11,625
  • 3
  • 43
  • 61
1
foreach (ListItem item in chkHobbies.Items)
{
   if (item.Selected == false)
   {
      // your code here
   }
}
Hitesh
  • 3,449
  • 8
  • 39
  • 57