1.5. Displaying an Array as a Group of Checkboxes
Problem
You have data in an array that needs to be displayed as a group of checkboxes.
Solution
Use a CheckBoxList control and bind the array to
it.
Add a CheckBoxList control to the
.aspx file.
In the code-behind class for the page, bind the array to the
CheckBoxList control.
Figure 1-4 shows the appearance of a typical
CheckBoxList in a browser, with a couple of
checkboxes preselected. Example 1-11 through
Example 1-13 show the .aspx and
code-behind files for an application that produces this result.

Figure 1-4. CheckBoxList with array data output
Discussion
The CheckBoxList control makes the job of
generating a list of checkboxes extremely easy.
Here’s a rundown of some of the attributes that
control the checkbox display. In the example that we developed for
this recipe, we have placed a CheckBoxList control
in a Table cell to control its position on the form, as shown in
Example 1-11.
The RepeatColumns attribute of the
CheckBoxList control is used to set the number of
columns in which the checkboxes are to be displayed.
The RepeatDirection attribute is set to
Horizontal, which displays the checkboxes in rows
from left-to-right and then top-to-bottom. This attribute can also be
set to Vertical to display the checkboxes in
columns from top-to-bottom and then left-to-right.
The RepeatLayout attribute is set to
Table, which causes the
CheckBoxList ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access