[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

Loop though checkbox list asp.net c#

Posted on 2007-10-01
3
Medium Priority
?
325 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?

0
Comment
Question by:mugsey
  • 2
3 Comments
 
LVL 27

Expert Comment

by:Sammy Ageil
ID: 19991039
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
 

Author Comment

by:mugsey
ID: 19991083
Thanks but I have about 20 checkboxes how do I know if say checkbox20 is equal to cookery?
0
 
LVL 27

Accepted Solution

by:
Sammy Ageil earned 2000 total points
ID: 19991142
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

Featured Post

New feature and membership benefit!

New feature! Upgrade and increase expert visibility of your issues with Priority Questions.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article aims to explain the working of CircularLogArchiver. This tool was designed to solve the buildup of log file in cases where systems do not support circular logging or where circular logging is not enabled
High user turnover can cause old/redundant user data to consume valuable space. UserResourceCleanup was developed to address this by automatically deleting user folders when the user account is deleted.
This lesson discusses how to use a Mainform + Subforms in Microsoft Access to find and enter data for payments on orders. The sample data comes from a custom shop that builds and sells movable storage structures that are delivered to your property. …
When cloud platforms entered the scene, users and companies jumped on board to take advantage of the many benefits, like the ability to work and connect with company information from various locations. What many didn't foresee was the increased risk…
Suggested Courses

831 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question