user3221917
user3221917

Reputation: 127

ASP.Net checkboxes and if statements

I am asked to develop an application where a member can select 1 plan, a combination of plans, all plans, or none.

plan1, plan2, plan3, .... plan12

I ran the truth table to find out how many possibilities there are, and it turned out to be 4096. (Ridiculous!)

My plan was to write an if statement for each possibility like this:

if (plan1.Checked == true && plan2.Checked == false && ... && plan12.Checked == false){
    // insert into into table test VALUES('Plan1')
}

and so on! Obviously, there must be a better and easier way than this. Any suggestions would help. Thank you all.

Upvotes: 2

Views: 926

Answers (2)

user3787846
user3787846

Reputation:

Andrew_CS answer is suitable and I prefer it, but here is another way:

Handle the checked event in the code behind of all checkboxes (use one event, but allow all checkboxes to be handled by it), and depending on the sender, then you can load the information you want.

It may be that you use a Select statement to load the relevant information for each checkbox during the checkbox checked event (might be a little easier to understand than a for loop!)

Upvotes: 0

Andrew_CS
Andrew_CS

Reputation: 2562

If you used a CheckBoxList or similar component this would be one approach.

CheckBoxList checkBoxList = ....;  // Just an example here
// You would add the different project names into the CheckBoxList

String message = "";

for (int i = 0; i < checkBoxList.Items.Count; i++) // Look at every project name
{
    if (checkBoxList.Items[i].Selected) // See if it's selected
    {
        message += checkBoxList.Items[i].Text; // Add the name to the message
    }
}

Put message in DB; // <-- Store the message into your database here

Since you're only interested in selected items you wouldn't have to deal with non-selected items at all.

Note: There is probably a better/more efficient way of creating strings than this and you might be able to use lambda expressions. This is just showing a simple approach.

Upvotes: 1

Related Questions