2

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.

3
  • 1
    You need to share your code in the question itself . Images are not readable . How are you populating CategoriesCBL with all the categories? Commented Apr 10, 2018 at 13:52
  • "Here's my code: i63.tinypic.com/2iactj7.png" No, on stackoverflow code is text not an image Commented Apr 10, 2018 at 13:52
  • So if this is anything like other controls, you only have one selected index. Do the checkbox's on the list have an IsChecked property you can set to true? Commented Apr 10, 2018 at 14:08

3 Answers 3

3

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

Sign up to request clarification or add additional context in comments.

2 Comments

Thank you for your answer! it worked. I thought I was close!
@HarryNicholls Ya, not too far off. I figured it would be something like this, but I haven't worked with asp.net much, so it took me a little while to find it.
2

I think you need something like this. You split the string to a List and then check with Linq if the items exits in CategoryCBL and then check the checkbox.

string categories = "Outcome Form, Executive Reporting, QlikView, Waiting Times";

List<string> categorie = categories.Split(',').ToList();

CategoryCBL.Items.Cast<ListItem>().ToList().ForEach(x => x.Selected = categorie.Any(y => y.Trim() == x.Value));

And if you need the checked items in a TextBox, you do this

TextBox1.Text = String.Join(", ", CategoryCBL.Items.Cast<ListItem>().Where(x => x.Selected).Select(y => y.Value).ToList());

Comments

0
<asp:CheckBoxList ID="tolgraph" runat="server" RepeatLayout="Table" CssClass="cb" RepeatDirection="Horizontal">
    <asp:ListItem Text="Column" Value="column"></asp:ListItem>
    <asp:ListItem Text="Line" Value="line"></asp:ListItem>
    <asp:ListItem Text="Bar" Value="bar"></asp:ListItem>
    <asp:ListItem Text="Pie" Value="pie"></asp:ListItem>
    <asp:ListItem Text="Radar" Value="Radar"></asp:ListItem>
    <asp:ListItem Text="Pareto" Value="Pareto"></asp:ListItem>
</asp:CheckBoxList>

tolgraph.Items.FindByText("Column").Selected = true;

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.