5.5. Using the DropDownList Control

The ASP.NET DropDownList control displays a large number of items in a very little space because it drops down to display its list when the user clicks the arrow. (Sometimes, it rises upward to display the items.)

At design-time, you can add static items to the DropDownList control by using the ListItem collection editor. At runtime, you can fill a DropDownList control with almost any data as long as you can get it into a simple list. To put color names in a DropDownList control, follow these steps:

  1. From the Toolbox, add a DropDownList, Label, and Panel control to an ASP.NET page.

  2. Select the DropDownList control and set its AutoPostBack property to True.

    AutoPostBack causes a page to submit its data to the Web server (and cause a postback) when the user merely selects a different item. No Submit button is required.

  3. Double-click the DropDownList control to create its default event handler and use the following code inside the SelectedIndexChanged subroutine:

    Dim strClr As String
    strClr = DropDownList1.SelectedValue
    Dim objColor As System.Drawing.Color
    objColor = _
     System.Drawing.ColorTranslator.FromHtml(strClr)
    Panel1.BackColor = objColor
    Label1.Text = strClr
  4. Return to Design view and double-click a blank area of the surface to create an event handler for the Page object's Load event and then add the following code above the final line of the Page_Load routine:

    If Not IsPostBack Then Dim enClr As System.Drawing.KnownColor Dim clrs As New _ System.Collections.Generic.List ...

Get ASP.NET 3.5 For Dummies® now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.