Programmatically Check an Item in Checkboxlist where text is equal to what i want

后端 未结 5 1001
旧巷少年郎
旧巷少年郎 2020-12-09 03:47

In C#, I am trying to Check an item in a CheckBoxList where the text equals what I require.

I would modify the code to check items that exist in the database.

<
5条回答
  •  自闭症患者
    2020-12-09 04:27

    I tried adding dynamically created ListItem and assigning the selected value.

    foreach(var item in yourListFromDB)
    {
     ListItem listItem = new ListItem();
     listItem.Text = item.name;
     listItem.Value = Convert.ToString(item.value);
     listItem.Selected=item.isSelected;                 
      checkedListBox1.Items.Add(listItem);
    }
    checkedListBox1.DataBind();
    

    avoid using binding the DataSource as it will not bind the checked/unchecked from DB.

提交回复
热议问题