tags:

views:

264

answers:

2

In our system, we need to pass along a pair of IDs for each checkbox selected (essentially an ordered pair). We are using a simple list of checkboxes via a CheckBoxList in ASP.NET. Is there an elegant way to do this?

A: 

One solution I have come up with is to use comma-separated (or colon-separated) values in each of the checkboxes, then just parse out both values when the form is submitted. Is there a more elegant way of doing this?

Secret Agent Man
A: 

I think comma-separating the IDs is the best solution, provided that the IDs are numbers.

Other options would be...

  • Store the ID pairs in an object in an arraylist, and then use the index of the object as the value for the checkbox. You could then pull out all the objects that are "selected", and then get the IDs.

  • With each checkbox, have two corresponding hidden field controls which store each of the IDs. You could then use the FindControl function to get the corresponding hidden field controls for each checkbox.

Each of these solutions would be more complex that just comma-separating the IDs, so I'd go with that!

philwilks