Check box if check box is checked in database

I am using C # .net.

Thanks in advance for your help.

I have searched the web but don't think I am using the correct words as nothing comes back, really helps.

I have an "edit" section in my web form that allows the user to enter (using a checklist) certain information.

For example:

• Receive newsletters • Receive phone calls, etc.

The checklist is populated from the Requirements database table.

When a user checks a specific checkbox, this information should be stored in another userRequirement table.

I can display all 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 automatically commit the checkboxes to the right?

Example:

  • The user selects "Receive mailings" check the box and click "Refresh". button.
  • It is then stored within the userRequirement table along with the user id
  • If the user wants to edit their details again, they can do. They are translated into the "edit page. Here" Receive newsletters "must be selected.

Should I use an if statement? If anyone can help by providing an example?

thank

Clare

+2


source to share


2 answers


You can loop through all the items in the CheckBoxList using a foreach loop, for example:

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

      



Then you can set whether the item is checked by setting the Selected property to true. Does it help anyone?

+1


source


In your loop, you can select the items you want as they are injected into the CheckBoxList. It might seem like 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);
        }

      

0


source







All Articles