Link to home
Start Free TrialLog in
Avatar of mugsey
mugseyFlag for United Kingdom of Great Britain and Northern Ireland

asked on

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?

Avatar of Sammy
Sammy
Flag of Canada image

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

ASKER

Thanks but I have about 20 checkboxes how do I know if say checkbox20 is equal to cookery?
ASKER CERTIFIED SOLUTION
Avatar of Sammy
Sammy
Flag of Canada image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial