Search code examples
c#asp.netcheckboxcheckboxlistlistbox-control

How to uncheck any checkbox ListItem After FindByText in C#?


I have a three Checkbox List on my page and I am adding any checked checkbox list Items to a List Box Control. When I remove any item from the List Box, it automatically UN-checks that list item on my checkbox List.

So far I am finding which checkbox list contains that checkbox and it's working. But I don't have an idea how I can uncheck that item which contains that text.

What I have tried so far is shown below:

if (listboxControl.SelectedIndex > 0)
{
    string na = listboxControl.SelectedItem.Text;
    listboxControl.Items.RemoveAt(listboxControl.SelectedIndex);
    var cb1 = CheckBoxList1.Items.FindByText(na);
    var cb2 = CheckBoxList2.Items.FindByText(na);
    var cb3 = CheckBoxList3.Items.FindByText(na);
    if (cb1 != null) 
    {
           //here how i can Uncheck That Item        
    }
    else if (cb2 != null)
    {
           //here how i can Uncheck That Item 
    }
    else if (cb3 != null)
    {
           //here how i can Uncheck That Item 
    }
    else
    {

    }
}

Solution

  • The method CheckBoxList1.Items.FindByText() will search the collection for a ListItem with a Text property that equals text specified by the text parameter. This method performs a case-sensitive and culture-insensitive comparison. This method does not do partial searches or wildcard searches. If an item is not found in the collection using these criteria, null is returned.

    So, the return value of this method will be the required item or null, If it is not null then you can use the Selected property to check/uncheck the item.

    if (cb1 != null) 
    {
       cb1.Selected = false;
    }