-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.

4
  • 2
    try this. stackoverflow.com/questions/18924147/… Commented Dec 12, 2014 at 6:33
  • @Kumar this is for selected values i want to get Unselected values :( Commented Dec 12, 2014 at 6:35
  • 2
    You can use Hitesh answer. Commented Dec 12, 2014 at 6:39
  • 2
    @Gitz What's the big deal in that? Instead of using item.Selected just use !item.Selected(note the not symbol before item) in the linked answer. Commented Dec 12, 2014 at 6:45

2 Answers 2

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 + ",";
            }
        }
Sign up to request clarification or add additional context in comments.

1 Comment

Once again saying that pleasure is all mine :)
1
foreach (ListItem item in chkHobbies.Items)
{
   if (item.Selected == false)
   {
      // your code here
   }
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.