Home   Cover Cover Cover Cover


From Section 6.4.10 of the book

This example shows how to build a DropDownList from statically specified list items. The items denote countries. If an item is selected the international car code of this country is displayed. As with a ListBox the items of a DropDownList can also be added dynamically or retrieved from a database.

<%@ Page Language="C#" %>
    <script Language="C#" Runat="server">
      void HandleSelect(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:DropDownList ID="list" OnSelectedIndexChanged="HandleSelect"
        AutoPostBack="true" Runat="server" >
        <asp:ListItem Text="United States" Value="USA" />
        <asp:ListItem Text="Great Britain" Value="GB" />
        <asp:ListItem Text="Germany" Value="D" />
        <asp:ListItem Text="France" Value="F" />
        <asp:ListItem Text="Italy" Value="I" />
      <asp:Label ID="lab" Runat="server" />

Try it