Delphi Programming

  1. Home
  2. Computing & Technology
  3. Delphi Programming
Understanding Web Controls for Selecting Choices in Delphi ASP.NET Applications
Page 2: ListBox, DropDownList
 More of this Feature
• Page 1: RadioButton, CheckBox
• Page 2: RadioButtonList, CheckBoxList
 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.

Creating lists
ListBox and DropDownList allow users to select textual items from a list. Depending on a situation, single or single (DropDownList) or multiple items (listBox) can be selected. Both the ListBox and the DropDownList operate much similar to the familiar Win32 VCL controls: TListBox and TComboBox (with Style := csDropDownList).
Both controls have the Items property, a collection of ListItem controls.
DropDownList
The DropDownList controls displays (enables to select) a single item at a time. It shares many properties of the RadioButtonList - it just looks differently. The DropDownList control renders as a <SELECT> HTML element with each item rendered as an <OPTION> HTML element.

When you place a DropDownList control on a web form, at design time, the "aspx" view will show:

  <asp:dropdownlist 
     id=DropDownList1 
     runat="server">
  </asp:dropdownlist>

Again, the SelectedIndexChanged event provides the ways to handle the change in the selected item upon page postback. As with the RadioButtonList and CheckBoxList, the Items property can be populated either at design-time (using the ListItemCollection Editor), or at run-time (my programmatically manipulating the Items collection) or by binding the control to some data source.

ListBox
The ListBox control provides the same functionality offered by the DropDownList control, except that it allows multiple selections of the items it holds. The SelectionMode determines how many items in the ListBox a user can select at one time.

The ListBox also renders as an <SELECT> HTML element with each item rendered as an <OPTION> HTML element.

One example of using the ListBox control was already provided in some of the previous chapters.

Here's one more fun example, follow the steps:

  1. Place one DropDownList, one ListBox and one Button control on a form
  2. Provide some items for the DropDownList in the Page_Load. By now, you know you have to use the IsPostback property of the form so that the code is executed only on the first visit to the page:

    procedure TWebForm6.Page_Load
              (sender: System.Object; 
               e: System.EventArgs);
    begin
      if NOT Page.IsPostBack then
      begin
        DropDownList1.Items.Add('Learning');
        DropDownList1.Items.Add('ASP.NET');
        DropDownList1.Items.Add('with Delphi');
        DropDownList1.Items.Add('IS fun!');
      end; //on no postback
    end;
    

  3. Assign the next code for the Click event of the button:

    procedure TWebForm6.Button1_Click
              (sender: System.Object; 
               e: System.EventArgs);
    var
      li:ListItem;
    begin
      li := DropDownList1.SelectedItem;
      if li <> nil then
      begin
        li.Selected := false; //explained in a minute
        ListBox1.Items.Add(li);
      end;
    end; //Button1_Click
    

  4. Run the project, select something in the DropDownList and hit the button:

    ListBox and DropDownList example

You might be asking why have I included the "li.Selected := false;" assignment in the code above. Try removing that line and run the code, after you try to move/copy/add the second item from the drop down list to the list box, you'll encounter a run time error:

A ListBox cannot have multiple items selected when the SelectionMode is Single.

What's this? We have not selected anything in the ListBox, yet the error is here ... very nasty one to be displayed to the user!

Note the code that adds (ListBox1.Items.Add(li);) items to the ListBox - the item selected (li := DropDownList1.SelectedItem;) in the DropDownList is added to the ListBox. The assignement creates an new ListItem by copying all the properties of the selected item in the drop down list. The Selected property of this item is true (otherwise it will not be selected) and such an item is added to the ListBox. If this is the second time we *copy* the item over to the list box - we'll try to create a situation where two items are selected in a list box with SelectionMode set to SelectionMode.One (default value).

One final note
Being able to populate the Items property from some data source (database, XML, etc.) is what makes the components discussed in this chapter so powerful. However, we are not going to discuss data binding in this chapter. Understanding data binding in ASP.NET is essential - we'll devote one large chapter in the near future to. Until then ... happy coding :)

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 ListBox.
When you use the ListBox (or DropDownList) 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: ListBoxItemAttributes Web Control.

To the next chapter: A Beginner's Guide to ASP.NET Programming for Delphi developers
That's it for today, in the next chapter we'll continue to explore Web Controls. Stay tuned!

If you need any kind of help at this point, please post to the Delphi Programming Forum where all the questions are answered and beginners are treated as experts.

First page > CheckBox, RadioButton > Page 1, 2, 3

A Beginner's Guide to ASP.NET Programming for Delphi developers: Next Chapter >>
>> Working with Container type ASP.NET controls in Delphi ASP.NET Applications

Explore Delphi Programming

About.com Special Features

Delphi Programming

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

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

All rights reserved.