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.
If you would like an example, I need to select the checklistbox item that equals to abc.
Assuming that the items in your CheckedListBox are strings:
for (int i = 0; i < checkedListBox1.Items.Count; i++)
{
if ((string)checkedListBox1.Items[i] == value)
{
checkedListBox1.SetItemChecked(i, true);
}
}
Or
int index = checkedListBox1.Items.IndexOf(value);
if (index >= 0)
{
checkedListBox1.SetItemChecked(index, true);
}
Example based on ASP.NET CheckBoxList
<asp:CheckBoxList ID="checkBoxList1" runat="server">
<asp:ListItem>abc</asp:ListItem>
<asp:ListItem>def</asp:ListItem>
</asp:CheckBoxList>
private void SelectCheckBoxList(string valueToSelect)
{
ListItem listItem = this.checkBoxList1.Items.FindByText(valueToSelect);
if(listItem != null) listItem.Selected = true;
}
protected void Page_Load(object sender, EventArgs e)
{
SelectCheckBoxList("abc");
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With