ClareBear
ClareBear

Reputation: 1523

Tick correct checkbox if 'checked' within the database

I am using c# .net.

Thanks in advance for any help.

I have searched the web, but don't think I am using the right words, as nothing being returned is really helping.

I have a 'edit' section within my web-form which allows the user to tick (using a checklist) certain information.

For example:

• Receive newsletters • Receive phone calls etc

The checklist is populated from a database table called Requirements.

When the user ticks a certain checkbox this information should be stored within another table userRequirement.

I can display all the requirements (from Requirements) by looping through and adding another item:

            foreach (tblRequirement singleRequirement in viewAllRequirement)
            {
                requirementCheckBoxList.Items.Add(new ListItem(singleRequirement.requirementName,singleRequirement.rrequirementID.ToString(),true));
            }

However how do I then loop through the userRequirement and automatical tick the right checkboxes?

For Example:

Should I be using a if statement? If so can anyone help by providing an example?

Thanks

Clare

Upvotes: 2

Views: 1047

Answers (2)

msergeant
msergeant

Reputation: 4801

In your loop, you can select the proper items as they're being entered into the CheckBoxList. Might look something like this (I don't know how your tblRequirement object works):

        foreach (tblRequirement singleRequirement in viewAllRequirement)
        {
            ListItem newItem = new ListItem(singleRequirement.requirementName,singleRequirement.rrequirementID.ToString(),true));

            //If item should be checked
            if(singleRequirement.Checked)
                newItem.Selected = true;

            requirementCheckBoxList.Items.Add(newItem);
        }

Upvotes: 0

Dan Diplo
Dan Diplo

Reputation: 25339

You can loop through all the items in the CheckBoxList using a foreach loop like so:

foreach (ListItem item in requirementCheckBoxLis.Items)
{
    item.Selected = true; // This sets the item to be Checked
}

You can then set whether an item is checked by setting its Selected property to true. Does that help any?

Upvotes: 1

Related Questions