1. Home
  2. Computing & Technology
  3. Delphi Programming
Understanding Web Controls for Selecting Choices in Delphi ASP.NET Applications
Page 2: RadioButtonList, CheckBoxList
 More of this Feature
• Page 1: RadioButton, CheckBox
• Page 3: ListBox, DropDownList
 Join the Discussion
"Post your views and comments to this chapter of the free Asp.Net Delphi Programming Course"
Discuss!
 Related Resources
• A Beginner's Guide to Asp.Net Programming for Delphi developers.TOC

While RadioButton and CheckBox ASP.NET Web server controls enable single value selection, their compound versions, CheckBoxList and RadioButtonList are designed to provide multiple selections to the user. As expected, the check boxes in the CheckBoxList can be selected in any combination, while radio buttons combined in one RadioButtonList are mutually exclusive.

Both the RadioButtonList and CheckBoxList inherit from an abstract ListControl class, and therefore share the common set of properties and events.

All items displayed in the list control are stored in the Items collection. You can specify or determine the index of selected item in the list control by using the SelectedIndex property. The properties of the selected item can be accessed by using the SelectedItem property.

The SelectedIndexChanged event is fired after a postback if the selected item in the control has changed. In case of the CheckBoxList you will need to iterate through all the items to determine which item is selected (the Selected property of a particular item hold this information).

Adding items to a CheckBoxList or a RadioButtonList
One of the most important features of those controls is that they are bindable. While we have not yet discussed data binding in ASP.NET, let's just say that being bindable (in case of CheckBoxList and RadioButtonList controls) means that Items can be populated from a data source.
Since we will devote several chapters to data binding in web applications, this time we'll skip binding the selection controls.

If we decide to skip data binding, we are left with two choices of populating a CheckBoxList or a RadioButtonList with their items.

Firstly, we can add items at design time using the ListItemCollection Editor:

ListItemCollection Editor

Suppose that the above was a screen shot of adding 3 items to a CheckBoxList. At design time, the "aspx" view will show:

  <asp:checkboxlist id=CheckBoxList1
        runat="server">
    <asp:listitem value="Borland">Delphi</asp:listitem>
    <asp:listitem value="Microsoft">VB</asp:listitem>
    <asp:listitem value="MS and Borland">C#</asp:listitem>
  </asp:checkboxlist>

Each item (a single check box) in a CheckBoxList is presented as an ListItem control. The Text property specifies the text displayed in the list control for the item. The Value property allows you to associate a value with the item in the list control, in addition to the text displayed in the control.

At run time, a CheckBoxList will render as a TABLE element if the RepeatLayout is set to RepeatLayout.Table (the default setting). If RepeatLayout.Flow is used, the list is rendered without any table structure (as a SPAN element containing INPUT elements)

Much similar to the CheckBoxList, the RadioButtonList operates on the same prinicples but renders as a set of radio buttons that have their GroupName property "pre-set".

Although we can form the entire list of items using the ListItemCollection Editor (at design time), nothing stops us from dynamically adding or modifying entries at run time as well.

Here's an example, and the steps to build it:

  1. Have one design-time CheckBoxList populated as in the above picture
  2. Drop a RadioButtonList on a Web Form
  3. Add one Label also
  4. The "design" should look like:

    CheckBoxList, RadioButtonListm, an example (desiugn-time)

  5. Use the Page_Load procedure to populate the RadioButtonList with items. Note that we can check the IsPostback property of the Page in order to fill the list only once. The ViewState will hold and transfer the values between postbacks

    procedure TWebForm5.Page_Load
        (sender: System.Object; 
         e: System.EventArgs);
    var
      idx : integer;
      sel : string;
    begin
      if NOT Page.IsPostBack then //initially
      begin
        with RadioButtonList1.Items do
        begin
          Add('Show VB');
          Add(ListItem.Create('Hide VB'));
        end;
        RadioButtonList1.SelectedIndex := 0;
        RadioButtonList1.AutoPostBack := true;
        Label1.Text := 'Nothing initially selected';
      end
      else //on every postback 
       ...
    

    The above code adds two option buttons to the list control and sets some properties. Note the way items are added to the radio button list at run time.

  6. Create the event handler for the RadioButtonList's SelectedIndexChanged event, and add the following code:

    procedure TWebForm5.
      RadioButtonList1_SelectedIndexChanged
      (sender: System.Object; e: System.EventArgs);
    var
      li : ListItem;
    begin
      if RadioButtonList1.SelectedIndex = 0 then
      begin
        li := ListItem.Create('VB','Microsoft');
        CheckBoxList1.Items.Add(li);
      end
      else
      begin
        li := CheckBoxList1.Items.FindByText('VB');
        if li <> nil then
          CheckBoxList1.Items.Remove(li);
      end;
    end;
    

    Basically, if the "Show VB" option button is clicked, the "VB" item is displayed (re-added if necessary) in the CheckBoxList, otherwise it gets removed from the Items collection.

  7. To provide some output, we'll use the Label control. Go back to the Page_Load procedure and add some more code to it:

      ...
      else //on every postback 
      begin
        with CheckBoxList1 do
        for idx := 0 to -1 + Items.Count do
        begin
          if Items[idx].Selected then
            sel := sel + String.Format('{0} : {1}{2}', 
                         Items[idx].Value, 
                         Items[idx].Text, 
                         '<br>');
        end;
        Label1.Text := sel;
      end;
    end; //Page_Load
    

  8. Run the project, and "play" with it...

    Run time

Both the RadioButtonList and the CheckBoxList with their Items property provide enough methods and properties to easily iterate through the list and decide on page actions depending on the item(s) being selected.

Attributes don't render?!
Even though we have not yet discussed the Attributes property of a web control, developers already experimenting with ASP.NET might break their heads trying to add a simple "onclick" attribute to an item of a CheckBoxList (for example).
When you use the CheckBoxList (or RadioButtonList) Web Server ASP.NET control, if you try to add custom attributes to an Item, the key-value pairs are not rendered to the result page. Here's how to fix this behavior: CheckBoxListItemAttributes Web Control.

Before we finish this chapter we have to introduce two more selection controls: the ListBox and the DropDownList.

Next page > ListBox, DropDownList > Page 1, 2, 3

A Beginner's Guide to ASP.NET Programming for Delphi developers: Next Chapter >>
>> TOC

Explore Delphi Programming
About.com Special Features

Reader's Choice Award Winners

What are the best instant messengers, apps, editors and more? You told us, for our 2010 technology awards program. More >

iPad Central

Is Apple's new tablet computer impractical, a must-have -- or both? We'll help you figure it out. More >

  1. Home
  2. Computing & Technology
  3. Delphi Programming

©2010 About.com, a part of The New York Times Company.

All rights reserved.