ASP.NET's RadioButton and CheckBox controls work fine, but they really provide no support for handling groups of related data. If you want to fill a list of either type of controls with data from a DataSet, for example, you'll need to fill each control's text individually. In addition, keeping track of which items in the list of individual controls have been selected is a chore. To make working with groups of these controls easier, ASP.NET provides the CheckBoxList and RadioButtonList controls. Both controls allow you to bind their lists to data sources and fill these lists at runtime. The major difference between the two is that you can select as many items as you require using the CheckBoxList control, but you can only select a single item using the RadioButtonList control. (This behavior is most likely what you'd expect, given the usage of these two types of input controls.) Imagine that you'd like to allow users to select a single region from a list of regions. You might like to allow a single selection (using a list of radio buttons) or multiple selections (using check boxes). The sample page, ListControls.aspx, provides this capability using both types of list controls (see Figure 22.1). Figure 22.1. Use the CheckBoxList and RadioButtonList controls to allow selections from a list of options. Each of the two buttons on ListControls.aspx calls a common procedure: RegionLoad. This procedure fills the list with data retrieved from the Regions table in the Northwind sample database. Private Sub btnRegionCheckList_Click( _ ByVal sender As System.Object, _ ByVal e As System.EventArgs) _ Handles btnRegionCheckList.Click RegionLoad(clstRegions) End Sub Private Sub btnRegionRadioList_Click( _ ByVal sender As System.Object, _ ByVal e As System.EventArgs) _ Handles btnRegionRadioList.Click RegionLoad(rlstRegions) End Sub The RegionLoad procedure accepts, as its parameter, a variable of the ListControl type. Because both the CheckBoxList and RadioButtonList controls inherit from the ListControl base class, either is a valid parameter for this procedure. (You could also pass in a ListBox control or DropDownList control, because those controls also inherit from the ListControl base class.) The RegionLoad procedure shown in Listing 22.1 uses code exactly like you've seen before for working with ListBox or DropDownList controls. Listing 22.1 Fill Lists with Region Information Using This Procedure Private Sub RegionLoad( _ ByVal ctlRegions As ListControl) Dim strSQL As String Dim strConn As String Dim ds As DataSet ' Build Connect and SQL strings strConn = "Provider=sqloledb;" & _ "Data Source=(local);" & _ "Initial Catalog=Northwind;User ID=sa" strSQL = _ "SELECT RegionID, RegionDescription " & _ "FROM Region" ds = GetDataSet(strSQL, strConn) With ctlRegions .DataTextField = "RegionDescription" .DataValueField = "RegionID" .DataSource = ds .DataBind() End With End Sub This procedure builds SQL and connection strings and then calls the GetDataSet procedure to retrieve a DataSet object. The code sets the DataTextField and DataValueField properties of the list control, sets the data source to the DataSet, and then calls the DataBind method to hook up the data and display the control. The GetDataSet procedure should look completely familiar to you by this point. This procedure accepts SQL and connection strings and returns a DataSet object: Private Function GetDataSet( _ ByVal SQL As String, _ ByVal ConnectionString As String) _ As DataSet Dim da As OleDbDataAdapter Dim ds As New DataSet() Try da = New OleDbDataAdapter(SQL, ConnectionString) da.Fill(ds) Catch Throw End Try Return ds End Function |