Home   Cover Cover Cover Cover

ListBox With Statically Specified Entries

From Section 6.4.9 of the book

This example shows how to build a ListBox from statically specified list items. The items denote countries. If an item is selected the international car code of this country is displayed.

<%@ Page Language="C#" %>
    <script Language="C#" Runat="server">
      void ButtonClick(object sender, EventArgs e) {
        lab.Text = "The selected country has the international car code ";
        if (list.SelectedItem != null) lab.Text += list.SelectedItem.Value;
    <form Runat="server">
      <asp:ListBox ID="list" Rows="3" Runat="server" >
        <asp:ListItem Text="United States" Value="USA" Runat="server" />
        <asp:ListItem Text="Great Britain" Value="GB" Runat="server" />
        <asp:ListItem Text="Germany" Value="D" Runat="server" />
        <asp:ListItem Text="France" Value="F" Runat="server" />
        <asp:ListItem Text="Italy" Value="I" Runat="server" />
      <asp:Button OnClick="ButtonClick" Text="Show" Runat="server" /><br>
      <asp:Label ID="lab" Runat="server" />

Try it