Search code examples
c#asp.netstring-matchingselectedindexchecklistbox

C# Selecting a CheckBoxList item(s) by matching against an Array of strings


I'm struggling to find a solution to my problem, I have spent a considerable amount of time trying alternative solutions to no avail. Any help or explanations would be greatly appreciated.

I have a SQL database which has a string field called "categories", this containing a list of categories separated by ',' e.g. Referrals, Outpatients.

So I want this list to be compared to a number of CheckListBoxes items (ID=CategoryCBL), anytime the category matches a CheckListBox item it needs to be selected.

Here's my code:

string categories = result.GetString(12).ToString();
string[] categorie = categories.Split(',');

 //loops through all seperated categories (cat) in categorie.
 foreach(string cat in categorie)
 {
     //loops through all list checkboxes 
     for(int index = 0; index <CategoryCBL.Items.Count; index++)
     {
         //gets the listcheck box string 
         string item = CategoryCBL.Items[index].ToString();
         //compare the list box string against the current Category looking for matches
         if (item == cat)
         {
             //if a match occures the list checkbox at that index is selected 
             CategoryCBL.SelectedIndex = index;

             TextBox1.Text += item + "-" + cat + "-" + index;
         }
     }
 }

Here's my Check list box code:

<asp:CheckBoxList ID="CategoryCBL" class="listItem" RepeatLayout="Table" RepeatColumns="2" RepeatDirection="Vertical" runat="server" Width="100%">
    <asp:ListItem>Referrals</asp:ListItem>
    <asp:ListItem>Outpatients</asp:ListItem>
    <asp:ListItem>Admissions/Discharges</asp:ListItem>
    <asp:ListItem>A&E</asp:ListItem>
    <asp:ListItem>Medical Records</asp:ListItem>
    <asp:ListItem>Outcome Form</asp:ListItem>
    <asp:ListItem>Data Quality</asp:ListItem>
    <asp:ListItem>Executive Reporting</asp:ListItem>
    <asp:ListItem>Infection Control</asp:ListItem>
    <asp:ListItem>Planning and Performance</asp:ListItem>
    <asp:ListItem>QlikView</asp:ListItem>
    <asp:ListItem>Theatres</asp:ListItem>
    <asp:ListItem>Waiting Times</asp:ListItem>
</asp:CheckBoxList>

So I take my categories as result.getString(12).ToString(); in this example it equals Infection Control,QlikView,Theatres

You can also see I've printed the result to TextBox1.

Here's the outcome of the above code

https://i.sstatic.net/XZYU6.jpg

as you can see ONLY Theatres is selected.

https://i.sstatic.net/eJdHW.jpg

The output in TextBox1 shows that there are 3 matches occurring at X index's, these index's which line up with the indexes of my individual checklist boxes.

I'd really like to know why only the last matches checklistbox is selected and not the previos 2 matches.

any ideas?

Thank you.


Solution

  • Set the Selected property to true for each item you need to be checked.

    foreach (ListItem item in CheckBoxList.Items)
    {
        item.Selected = true;
    }
    

    In your code:

    //loops through all list checkboxes 
     for(int index = 0; index <CategoryCBL.Items.Count; index++)
     {
         //gets the listcheck box string 
         string item = CategoryCBL.Items[index].ToString();
         //compare the list box string against the current Category looking for matches
         if (item == cat)
         {
             //if a match occures the list checkbox at that index is selected 
             CategoryCBL.Items[index].Selected= true;
    
             TextBox1.Text += item + "-" + cat + "-" + index;
         }
     }
    

    I think this question is similar and might have other and better answers Check multiple items in ASP.NET CheckboxList