views:

272

answers:

2

Hello all,

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:

  • User selects ‘Receive Newsletters’ checkbox and presses the ‘Update’ button.
  • This is then stored within the userRequirement table along with the users ID
  • If the user wants to edit their details again, they can do. They are taken to the ‘edit’ page. Here the ‘Receive Newslettlers’ should already be selected.

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

Thanks

Clare

+1  A: 

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?

Dan Diplo
A: 

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);
        }
msergeant