Working with the CheckBoxList Control


Working with the CheckBoxList Control

The CheckBoxList control renders a list of check boxes. The check boxes can be rendered horizontally or vertically. Unlike the other List controls, a user always can select multiple items when using a CheckBoxList control.

For example, the page in Listing 10.13 contains a CheckBoxList control that renders its list items in two columns (see Figure 10.12).

Figure 10.12. Displaying list items with the CheckBoxList control.


Listing 10.13. ShowCheckBoxList.aspx

<%@ Page Language="VB" %> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd"> <script runat="server">     Protected  Sub btnSubmit_Click(ByVal sender As Object, ByVal e As EventArgs)         For Each item As ListItem In cblMovies.Items             if (item.Selected)                 lblMovie.Text &= "<li>" & item.Text             End If         Next     End Sub </script> <html xmlns="http://www.w3.org/1999/xhtml" > <head  runat="server">     <title>Show CheckBoxList</title> </head> <body>     <form  runat="server">     <div>     <asp:CheckBoxList                  DataSource         DataTextField="Title"         DataValueField="Id"         RepeatColumns="2"         Runat="server" />     <p>     <asp:Button                  Text="Submit"         OnClick="btnSubmit_Click"         Runat="server" />     </p>     <hr />     <asp:Label                  EnableViewState="false"         Runat="server" />     <asp:SqlDataSource                  SelectCommand="SELECT Id, Title FROM Movies"         ConnectionString="<%$ ConnectionStrings:Movies %>"         Runat="server" />     </div>     </form> </body> </html>

When you click the Submit button, the values of the Text property of any selected check boxes are displayed in a Label control. The selected check boxes are retrieved from the CheckBoxList control's Items property.

The CheckBoxList control includes three properties that affect its layout:

  • RepeatColumns The number of columns of check boxes to display.

  • RepeatDirection The direction in which the check boxes are rendered. Possible values are Horizontal and Vertical.

  • RepeatLayout Determines whether the check boxes are displayed in an HTML table. Possible values are Table and Flow.

Normally, a CheckBoxList control renders its list items in an HTML table. When the RepeatLayout property is set to the value Flow, the items are not rendered in a table.




ASP. NET 2.0 Unleashed
ASP.NET 2.0 Unleashed
ISBN: 0672328232
EAN: 2147483647
Year: 2006
Pages: 276

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net