• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 328
  • Last Modified:

Loop though checkbox list asp.net c#

I have a checkbox list control that contains about 20 checkboxes that capture an individuals interests.  For example, fishing, music, cookery etc

I need to use these checkboxes as a search for a dynamic query to sql server.  So if "cookery" is checked then I alter my sql query like this

   if (checkbox number 23 is checked)       //it must be cookery so add it to our WHERE clause
            {
                oClause.column = "[Cookery]";
                oClause.operand = " = ";
                oClause.criteria = "1";
                cClauses.Add(oClause);
            }

What is the best way to loop through the checkbox list to add checked items to my sql query?

0
mugsey
Asked:
mugsey
  • 2
1 Solution
 
SammyCommented:
foreach (Control ctrl in this.Controls)
        {
            if (ctrl is CheckBox)
            {
                CheckBox chk = (CheckBox)ctrl as CheckBox;
                if (chk.Checked)
                {
                    //Code to add to query
                }
            }
        }
0
 
mugseyAuthor Commented:
Thanks but I have about 20 checkboxes how do I know if say checkbox20 is equal to cookery?
0
 
SammyCommented:
Unless you have multiple checkboxes with the same values then you dont need to know which textBox has the value of cookery since there  is only one checkbox with this value
you can use the Text Property of the CheckBox to get the value
  foreach (Control ctrl in this.Controls)
        {
            if (ctrl is CheckBox)
            {
                CheckBox chk = (CheckBox)ctrl as CheckBox;
                if (chk.Text=="cookery")
                {
                   
                    //Code to add to query
                }
            }
        }
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now