Recipe 2.6. 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 2-4 shows the appearance of a typical CheckBoxList in a browser, with a couple of checkboxes preselected. Examples 2-11, 2-12 through 2-13 show the .aspx and code-behind files for an application that produces this result.

Figure 2-4. CheckBoxList with array data output


Discussion

The CheckBoxList control simplifies the job of generating a list of checkboxes. 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 2-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 control to output an HTML table that contains the checkboxes. Using Table ensures the checkboxes are aligned vertically. This attribute can be set to Flow, which causes the CheckBoxList control to output a <span> element for the checkboxes with <br> elements, thus placing the checkboxes in rows. In this case, unless all of your data is the same size, the checkboxes will not be aligned vertically.

The CssClass attribute controls the format of the text displayed with the checkboxes, and the width attribute sets the width of the generated HTML table.

The styles attribute can be used to format the data in any manner supported by inline HTML styles. If you're considering using inline styles though, remember that some older browsers do not fully support them.


If you need a list of radio buttons instead of checkboxes, substitute RadioButtonList for CheckBoxList in the .aspx file.


The Page_Load method in the code-behind, shown in Examples 2-12 (VB) and 2-13 (C#), builds the array of data by declaring an ArrayList and adding the text for the checkboxes to the ArrayList. It then sets the source of the data to the ArrayList and performs a data bind.

The ArrayList class provides a convenient, lightweight container for data that is to be bound to list controls. An ArrayList is almost identical to the built-in Array type but adds automatic size management that makes it easier to use.

To preselect a single checkbox, set the SelectedIndex property of the CheckBoxList control to the index of the item that is to be preselected. If multiple checkboxes need to be preselected, use the FindByText (or FindByValue) method of the Items collection in the CheckBoxList control to find the appropriate item(s), and then set the Selected property to true, as shown in Examples 2-12 (VB) and 2-13 (C#).

See Also

The ArrayList class in the MSDN Library

Example 2-11. CheckBoxList with array data (.aspx)

 <%@ Page Language="VB" MasterPageFile="~/ASPNetCookbookVB.master" AutoEventWireup="false" CodeFile="CH02CheckboxListWithArrayVB.aspx.vb" Inherits="ASPNetCookbook.VBExamples.CH02CheckboxListWithArrayVB" Title="Checkbox List With Array" %> <asp:Content  Runat="server" ContentPlaceHolder> <div align="center" > CheckBoxList With Array (VB) </div> <div style="margin-left:50px"> <asp:CheckBoxList  runat="server"  RepeatColumns="2"  RepeatDirection="Horizontal"  RepeatLayout="Table"  Css  TextAlign="Right"  width="100%" /> </div> </asp:Content> 

Example 2-12. CheckBoxList with array data code-behind (.vb)

 Option Explicit On Option Strict On Imports Microsoft.VisualBasic Imports System.Configuration Imports System.Data Imports System.Data.OleDb Namespace ASPNetCookbook.VBExamples ''' <summary> ''' This class provides the code behind for ''' CH02CheckboxListWithArrayVB.aspx ''' </summary> Partial Class CH02CheckboxListWithArrayVB Inherits System.Web.UI.Page '''*********************************************************************** ''' <summary> ''' This routine provides the event handler for the page load event. It ''' is responsible for initializing the controls on the page. ''' </summary> ''' ''' <param name="sender">Set to the sender of the event</param> ''' <param name="e">Set to the event arguments</param> Private Sub Page_Load(ByVal sender As Object, _   ByVal e As System.EventArgs) Handles Me.Load Dim values As ArrayList If (Not Page.IsPostBack) Then 'build array of data to bind to checkboxlist values = New ArrayList values.Add("Access Cookbook") values.Add("ASP.NET Cookbook") values.Add("Perl Cookbook") values.Add("Java Cookbook") values.Add("VB .Net Language in a Nutshell") values.Add("Programming Visual Basic .Net") values.Add("Programming C#") values.Add(".Net Framework Essentials") values.Add("COM and .Net Component Services") 'bind the data to the checkboxlist cbBooks.DataSource = values cbBooks.DataBind() 'preselect several books cbBooks.Items.FindByText("ASP.NET Cookbook").Selected = True cbBooks.Items.FindByText("Programming C#").Selected = True End If End Sub 'Page_Load End Class 'CH02CheckboxListWithArrayVB End Namespace 

Example 2-13. CheckBoxList with array data code-behind (.cs)

 using System; using System.Collections; using System.Configuration; using System.Web.UI.WebControls; namespace ASPNetCookbook.CSExamples { /// <summary> /// This class provides the code behind for /// CH02CheckboxListWithArrayCS.aspx /// </summary> public partial class CH02CheckboxListWithArrayCS : System.Web.UI.Page { /// <summary> /// This routine provides the event handler for the page load event. /// It is responsible for initializing the controls on the page. /// </summary> /// <param name="sender">Set to the sender of the event</param> /// <param name="e">Set to the event arguments</param> protected void Page_Load(object sender, EventArgs e) { ArrayList values; if (!Page.IsPostBack) { // build array of data to bind to checkboxlist values = new ArrayList(); values.Add("Access Cookbook"); values.Add("ASP.NET Cookbook"); values.Add("Perl Cookbook"); values.Add("Java Cookbook"); values.Add("VB .Net Language in a Nutshell"); values.Add("Programming Visual Basic .Net"); values.Add("Programming C#"); values.Add(".Net Framework Essentials"); values.Add("COM and .Net Component Services"); // bind the data to the checkboxlist cbBooks.DataSource = values; cbBooks.DataBind(); // preselect several books cbBooks.Items.FindByText("ASP.NET Cookbook").Selected = true; cbBooks.Items.FindByText("Programming C#").Selected = true; } } // Page_Load } // CH02CheckboxListWithArrayCS } 



ASP. NET Cookbook
ASP.Net 2.0 Cookbook (Cookbooks (OReilly))
ISBN: 0596100647
EAN: 2147483647
Year: 2003
Pages: 202

Similar book on Amazon

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