简体   繁体   中英

Get unchecked values from CheckBoxList in c#

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 .

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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