Programming Data-Driven Web Applications with ASP.NET
CheckBoxList ASP.NET Server Control"-->
| only for RuBoard |
CheckBoxList ASP.NET Server Control
The CheckBoxList server control differs from the previous CheckBox control in that, when the CheckBox control is used, a single Checkbox control is rendered. On the other hand a single CheckBoxList control can render multiple check boxes. The CheckBoxList is another one of the controls that is part of the List controls suite.
The CheckBox control does give you greater flexibility in positioning than a CheckBoxList does because it renders as a list. However, the CheckBox 's data binding support is limited when compared to the CheckBoxList because it fully supports data binding using its DataSource , DataTextField , and DataValueField properties.
The CheckBoxList does give you some control over how its list is created by exposing some style attributes. You'll notice that these style attributes are also found in the DataList control. The following list contains a description of each:
-
RepeatColumns The number of columns to display ”if set to three, CheckBox es will render three across
-
RepeatDirection Controls which direction items from the data source are rendered ”values can be Horizontal or Vertical
-
RepeatLayout Determines whether the list is rendered within a TABLE or SPAN ”values can be Table or Flow
If RepeatLayout is set to Table , then two other attributes become available:
-
CellPadding The CellPadding for the container Table
-
CellSpacing The CellSpacing for the container Table
Listing 8.18 demonstrates how to bind the CheckBoxList to a data source and what attributes you can use to affect the way the control is rendered.
Listing 8.18 Using the CheckBoxList Control
[VisualBasic.NET] 01: <%@ Import Namespace="System.Data" %> 02: <%@ Import Namespace="System.Data.SqlClient" %> 03: <script runat="server" language="vb" > 04: 05: public sub Page_Load(sender as Object, e as EventArgs) 06: 07: if (not IsPostBack) then 08: cbl_DataBind() 09: end if 10: 11: end sub 12: 13: public sub cbl_DataBind() 14: 15: dim SqlCon as new SqlConnection("server=localhost; uid=sa;pwd=;database=northwind") 16: dim SqlCmd as new SqlCommand("SELECT TOP 21 ProductName, ProductID FROM Products",
Listing 8.18 renders the page illustrated in Figure 8.9.
Figure 8.9. The rendered CheckBoxList control.
In this example I have RepeatLayout set to Table (line 48) so each CheckBox is in its own TableCell. RepeatColumns is set to 3 (line 49) so the control is rendered three columns wide. RepeatDirection is set to Vertical , so items are ordered from the top to the bottom from the data source. I suggest playing around with the different values you can use for these three attributes so you can see the effects of each value. For instance, change RepeatLayout to Flow and leave all the other attributes the same.
Raising and Handling Events and Determining Item Selection
In this section, we'll demonstrate how to respond and handle events with the CheckBoxList control. Because the way you raise and handle events with the CheckBoxList is identical to that of the other List controls, we won't go into great detail explaining it. Listing 8.19 contains example code illustrating how to determine item selection on a post back.
Listing 8.19 Handling Events and Determining Item Selection
[VisualBasic.NET] 01: <%@ Import Namespace="System.Data" %> 02: <%@ Import Namespace="System.Data.SqlClient" %> 03: <script runat="server" language="vb" > 04: 05: public sub Page_Load(sender as Object, e as EventArgs) 06: 07: if (not IsPostBack) then 08: cbl_DataBind() 09: end if 10: 11: end sub 12: 13: public sub cbl_DataBind() 14: 15: dim SqlCon as new SqlConnection("server=localhost; uid=sa;pwd=;database=northwind") 16: dim SqlCmd as new SqlCommand("SELECT TOP 21 ProductName, ProductID FROM Products",
When Listing 8.19 is executed and a check box is selected, the resulting page is posted back, and the Label control under the CheckBoxList control is populated with the names of the products next to all selected check boxes. Figure 8.10 shows a page with a few check boxes selected.
Figure 8.10. The Label control under the CheckBoxList control lists all currently selected items.
| only for RuBoard |