We help IT Professionals succeed at work.

Loop though checkbox list asp.net c#

mugsey
mugsey asked
on
355 Views
Last Modified: 2013-11-26
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?

Comment
Watch Question

CERTIFIED EXPERT

Commented:
foreach (Control ctrl in this.Controls)
        {
            if (ctrl is CheckBox)
            {
                CheckBox chk = (CheckBox)ctrl as CheckBox;
                if (chk.Checked)
                {
                    //Code to add to query
                }
            }
        }

Author

Commented:
Thanks but I have about 20 checkboxes how do I know if say checkbox20 is equal to cookery?
CERTIFIED EXPERT
Commented:
Unlock this solution and get a sample of our free trial.
(No credit card required)
UNLOCK SOLUTION
Unlock the solution to this question.
Thanks for using Experts Exchange.

Please provide your email to receive a sample view!

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.