How List Box Values Can Be Accessed in Form Processing Scripts

Last Updated: Nov 02, 2025
7 min read
Legacy Archive
Legacy Guidance: This article preserves historical web development content. For modern .NET 8+ best practices, visit our Tutorials section.

Introduction

There are times when a text box just won't cut it. Perhaps you want to restrict users to a specific set of choices. For example, if you want users to specify their state of residency, you don't want to use a text box because someone might misspell a state's name or enter "41" as their state. When you need users to choose from a particular set of valid options, it's best to use a list box.

Understanding List Box Structure

Of all the form field types, the list box is the oddball. It's the only one that isn't created using the <INPUT> tag. Instead, the list box uses two tags: the <SELECT> and <OPTION> tags. The <SELECT> tag indicates that a list box will be created, while each <OPTION> tag represents a unique choice for the list box.

The SELECT Tag Properties

The <SELECT> tag has two important properties: NAME and SIZE.

The NAME property uniquely identifies the particular list box, just like with text box form fields. This is how you'll access the selected value in your form processing script.

The SIZE property determines how many list options are shown at one time. The default value for SIZE is 1, which means a list box will show only one option at a time (appearing as a dropdown). When you set SIZE to a larger number, it displays as a scrollable list.

Here's a basic example:

HTML Dropdown and Scrollable List
<!-- Dropdown list (SIZE = 1 by default) -->
<SELECT NAME="country">
    <OPTION VALUE="us">United States</OPTION>
    <OPTION VALUE="uk">United Kingdom</OPTION>
    <OPTION VALUE="ca">Canada</OPTION>
</SELECT>

<!-- Scrollable list (SIZE = 3) -->
<SELECT NAME="state" SIZE="3">
    <OPTION VALUE="CA">California</OPTION>
    <OPTION VALUE="NY">New York</OPTION>
    <OPTION VALUE="TX">Texas</OPTION>
    <OPTION VALUE="FL">Florida</OPTION>
</SELECT>

The OPTION Tag Properties

The <OPTION> tag has two important properties: VALUE and the displayed text.

The VALUE property uniquely identifies each separate list box option. When the user selects a list box option and submits the form, the form processing script receives the string in the VALUE property of the selected item.

It's important to note that the VALUE property doesn't determine what's displayed in the list box. The text between <OPTION> and </OPTION> is what users see, while the VALUE is what your script receives.

Here's a complete example:

Complete HTML Form with List Boxes
<HTML>
<BODY>
    <FORM METHOD="POST" ACTION="/Scripts/ProcessForm.asp">
        <h3>How much do you like ASP?</h3>
        <SELECT NAME="ASPRating">
            <OPTION VALUE="5">I like ASP a lot!</OPTION>
            <OPTION VALUE="4">ASP sure is neat</OPTION>
            <OPTION VALUE="3">It's interesting</OPTION>
            <OPTION VALUE="2">ASP is difficult</OPTION>
            <OPTION VALUE="1">Not a fan</OPTION>
        </SELECT>
        
        <h3>How many years of ASP experience do you have?</h3>
        <SELECT NAME="Experience" SIZE="5">
            <OPTION VALUE="10">10+ years of ASP experience</OPTION>
            <OPTION VALUE="9">9 years of ASP experience</OPTION>
            <OPTION VALUE="8">8 years of ASP experience</OPTION>
            <OPTION VALUE="7">7 years of ASP experience</OPTION>
            <OPTION VALUE="6">6 years of ASP experience</OPTION>
            <OPTION VALUE="5">5 years of ASP experience</OPTION>
            <OPTION VALUE="4">4 years of ASP experience</OPTION>
            <OPTION VALUE="3">3 years of ASP experience</OPTION>
            <OPTION VALUE="2">2 years of ASP experience</OPTION>
            <OPTION VALUE="1">1 year of ASP experience</OPTION>
            <OPTION VALUE="0">Less than a year</OPTION>
        </SELECT>
        
        <p><INPUT TYPE="submit" VALUE="Submit"></p>
    </FORM>
</BODY>
</HTML>

Accessing List Box Values in Classic ASP

When a user submits the form, you can access the selected values in your ASP script using the Request object:

Classic ASP Form Processing
<%
' Get the selected rating
Dim rating
rating = Request.Form("ASPRating")

' Get the selected experience level
Dim experience
experience = Request.Form("Experience")

' Display the results
Response.Write "You rated ASP: " & rating & "<br>"
Response.Write "Years of experience: " & experience & "<br>"

' You can also use the values in conditional logic
If CInt(rating) >= 4 Then
    Response.Write "<p>Great! You really enjoy ASP!</p>"
ElseIf CInt(rating) >= 3 Then
    Response.Write "<p>You have a neutral opinion about ASP.</p>"
Else
    Response.Write "<p>Sorry to hear you're not enjoying ASP.</p>"
End If
%>

Setting a Default Selected Option

You can set a default selected option using the SELECTED attribute:

HTML Default Selection
<SELECT NAME="country">
    <OPTION VALUE="us" SELECTED>United States</OPTION>
    <OPTION VALUE="uk">United Kingdom</OPTION>
    <OPTION VALUE="ca">Canada</OPTION>
    <OPTION VALUE="au">Australia</OPTION>
</SELECT>

When the page loads, "United States" will be selected by default.

Creating Multi-Select List Boxes

You can allow users to select multiple options by adding the MULTIPLE attribute:

HTML Multi-Select List
<SELECT NAME="interests" SIZE="5" MULTIPLE>
    <OPTION VALUE="web">Web Development</OPTION>
    <OPTION VALUE="mobile">Mobile Development</OPTION>
    <OPTION VALUE="database">Database Design</OPTION>
    <OPTION VALUE="security">Security</OPTION>
    <OPTION VALUE="cloud">Cloud Computing</OPTION>
</SELECT>

Accessing multiple selections in Classic ASP:

ASP Multi-Select Processing
<%
' Get all selected values (comma-separated)
Dim interests
interests = Request.Form("interests")

' Split into an array
Dim interestsArray
interestsArray = Split(interests, ", ")

Response.Write "<h3>Your selected interests:</h3><ul>"
For Each interest In interestsArray
    Response.Write "<li>" & interest & "</li>"
Next
Response.Write "</ul>"
%>

Modern ASP.NET Implementation

In ASP.NET Web Forms, you'd use the DropDownList or ListBox controls:

ASP.NET DropDownList:

ASP.NET DropDownList
<!-- ASP.NET DropDownList -->
<asp:DropDownList ID="ddlCountry" runat="server">
    <asp:ListItem Value="us" Text="United States" />
    <asp:ListItem Value="uk" Text="United Kingdom" />
    <asp:ListItem Value="ca" Text="Canada" />
</asp:DropDownList>

Code-behind to access the value:

ASP.NET Code-Behind
protected void SubmitButton_Click(object sender, EventArgs e)
{
    string selectedCountry = ddlCountry.SelectedValue;
    string selectedText = ddlCountry.SelectedItem.Text;
    
    Response.Write($"Selected Value: {selectedCountry}<br>");
    Response.Write($"Selected Text: {selectedText}");
}

Populating list boxes from a database:

ASP.NET Database Population
protected void Page_Load(object sender, EventArgs e)
{
    if (!IsPostBack)
    {
        LoadCountries();
    }
}

private void LoadCountries()
{
    using (SqlConnection conn = new SqlConnection(connectionString))
    {
        string query = "SELECT CountryCode, CountryName FROM Countries ORDER BY CountryName";
        SqlCommand cmd = new SqlCommand(query, conn);
        
        conn.Open();
        SqlDataReader reader = cmd.ExecuteReader();
        
        ddlCountry.DataSource = reader;
        ddlCountry.DataTextField = "CountryName";
        ddlCountry.DataValueField = "CountryCode";
        ddlCountry.DataBind();
        
        reader.Close();
    }
    
    // Add a default "Please select" option
    ddlCountry.Items.Insert(0, new ListItem("-- Please Select --", ""));
}

ASP.NET MVC Implementation

In ASP.NET MVC, you'd typically use the @Html.DropDownList helper:

View Model:

MVC View Model
public class RegistrationViewModel
{
    public string SelectedCountry { get; set; }
    public IEnumerable<SelectListItem> Countries { get; set; }
}

Controller:

MVC Controller
public ActionResult Register()
{
    var model = new RegistrationViewModel
    {
        Countries = GetCountries()
    };
    return View(model);
}

private IEnumerable<SelectListItem> GetCountries()
{
    return new List<SelectListItem>
    {
        new SelectListItem { Value = "us", Text = "United States" },
        new SelectListItem { Value = "uk", Text = "United Kingdom" },
        new SelectListItem { Value = "ca", Text = "Canada" },
        new SelectListItem { Value = "au", Text = "Australia" }
    };
}

[HttpPost]
public ActionResult Register(RegistrationViewModel model)
{
    if (ModelState.IsValid)
    {
        string selectedCountry = model.SelectedCountry;
        // Process the form
        return RedirectToAction("Success");
    }
    
    model.Countries = GetCountries();
    return View(model);
}

View:

MVC View
@model RegistrationViewModel

@using (Html.BeginForm())
{
    <div class="form-group">
        <label for="SelectedCountry">Country:</label>
        @Html.DropDownListFor(m => m.SelectedCountry, 
                              Model.Countries, 
                              "-- Please Select --", 
                              new { @class = "form-control" })
        @Html.ValidationMessageFor(m => m.SelectedCountry)
    </div>
    
    <button type="submit" class="btn btn-primary">Submit</button>
}

ASP.NET Core MVC with Tag Helpers

In ASP.NET Core, you can use tag helpers for cleaner syntax:

View Model:

Core View Model
public class FormViewModel
{
    [Required]
    [Display(Name = "Country")]
    public string SelectedCountry { get; set; }
    
    public List<SelectListItem> Countries { get; set; }
}

Controller:

Core Controller
public IActionResult Index()
{
    var model = new FormViewModel
    {
        Countries = new List<SelectListItem>
        {
            new SelectListItem { Value = "us", Text = "United States" },
            new SelectListItem { Value = "uk", Text = "United Kingdom" },
            new SelectListItem { Value = "ca", Text = "Canada" }
        }
    };
    return View(model);
}

[HttpPost]
public IActionResult Index(FormViewModel model)
{
    if (ModelState.IsValid)
    {
        // Access the selected value
        string country = model.SelectedCountry;
        return RedirectToAction("Success");
    }
    
    // Reload the dropdown options
    model.Countries = LoadCountries();
    return View(model);
}

View with Tag Helpers:

Core View with Tag Helpers
<form asp-action="Index" method="post">
    <div class="form-group">
        <label asp-for="SelectedCountry"></label>
        <select asp-for="SelectedCountry" asp-items="Model.Countries" class="form-control">
            <option value="">-- Please Select --</option>
        </select>
        <span asp-validation-for="SelectedCountry" class="text-danger"></span>
    </div>
    <button type="submit" class="btn btn-primary">Submit</button>
</form>

Grouping Options with OPTGROUP

You can organize options into groups using the <OPTGROUP> tag:

HTML OPTGROUP Example
<SELECT NAME="location">
    <OPTGROUP LABEL="United States">
        <OPTION VALUE="ny">New York</OPTION>
        <OPTION VALUE="ca">California</OPTION>
        <OPTION VALUE="tx">Texas</OPTION>
    </OPTGROUP>
    <OPTGROUP LABEL="Canada">
        <OPTION VALUE="on">Ontario</OPTION>
        <OPTION VALUE="bc">British Columbia</OPTION>
        <OPTION VALUE="qc">Quebec</OPTION>
    </OPTGROUP>
    <OPTGROUP LABEL="United Kingdom">
        <OPTION VALUE="eng">England</OPTION>
        <OPTION VALUE="sct">Scotland</OPTION>
        <OPTION VALUE="wls">Wales</OPTION>
    </OPTGROUP>
</SELECT>

Validating List Box Selections

Always validate user selections on the server side:

Classic ASP validation:

ASP Validation
<%
Dim selectedValue
selectedValue = Request.Form("country")

If selectedValue = "" Then
    Response.Write "<p style='color: red;'>Please select a country.</p>"
Else
    ' Valid options
    Dim validOptions
    validOptions = Array("us", "uk", "ca", "au")
    
    Dim isValid
    isValid = False
    For Each validOption In validOptions
        If selectedValue = validOption Then
            isValid = True
            Exit For
        End If
    Next
    
    If isValid Then
        Response.Write "<p style='color: green;'>Valid selection: " & selectedValue & "</p>"
    Else
        Response.Write "<p style='color: red;'>Invalid selection.</p>"
    End If
End If
%>

ASP.NET validation:

ASP.NET Validation
public ActionResult ProcessForm(string country)
{
    if (string.IsNullOrEmpty(country))
    {
        ModelState.AddModelError("country", "Please select a country.");
        return View();
    }
    
    // Validate against allowed values
    var validCountries = new[] { "us", "uk", "ca", "au" };
    if (!validCountries.Contains(country))
    {
        ModelState.AddModelError("country", "Invalid country selection.");
        return View();
    }
    
    // Process valid selection
    return RedirectToAction("Success");
}

Dynamic List Box Updates with JavaScript

You can create dependent dropdowns using JavaScript:

JavaScript Dependent Dropdowns
<script>
function updateCities() {
    var state = document.getElementById("stateSelect").value;
    var citySelect = document.getElementById("citySelect");
    
    // Clear existing options
    citySelect.innerHTML = '<option value="">-- Select City --</option>';
    
    // Define cities for each state
    var cities = {
        "CA": ["Los Angeles", "San Francisco", "San Diego"],
        "NY": ["New York City", "Buffalo", "Rochester"],
        "TX": ["Houston", "Dallas", "Austin"]
    };
    
    // Populate city dropdown
    if (cities[state]) {
        cities[state].forEach(function(city) {
            var option = document.createElement("option");
            option.value = city.toLowerCase().replace(/\s+/g, '-');
            option.text = city;
            citySelect.add(option);
        });
    }
}
</script>

<SELECT NAME="state" ID="stateSelect" onchange="updateCities()">
    <option value="">-- Select State --</option>
    <OPTION VALUE="CA">California</OPTION>
    <OPTION VALUE="NY">New York</OPTION>
    <OPTION VALUE="TX">Texas</OPTION>
</SELECT>

<SELECT NAME="city" ID="citySelect">
    <option value="">-- Select City --</option>
</SELECT>

Best Practices for List Boxes

Always include a default "Please select" option to ensure users make an active choice.

Use meaningful VALUE attributes that your processing script can easily work with.

Keep the displayed text user-friendly while using technical codes for values.

For long lists, consider using SIZE to show multiple options or implement search functionality.

Always validate selections on the server side, even if you have client-side validation.

Consider accessibility by using proper labels and ARIA attributes.

Group related options using <OPTGROUP> for better organization.

Wrapping Up

List boxes are powerful form controls that help you collect structured data from users. By understanding how to create them properly and access their values in your form processing scripts, you can build more robust and user-friendly web applications. Whether you're working with Classic ASP, ASP.NET Web Forms, or modern ASP.NET Core, the principles remain the same: provide clear options, validate user input, and handle the data securely.

Quick FAQ

How do I access a selected value from a list box in Classic ASP?

In Classic ASP, use the Request.Form collection with the list box's NAME attribute, e.g., Request.Form('listName'). This returns the VALUE of the selected OPTION. For multi-select, it returns comma-separated values.

What's the difference between DropDownList and ListBox in ASP.NET?

DropDownList in ASP.NET renders as a single-select dropdown (SIZE=1), while ListBox renders as a scrollable list (SIZE>1) and supports MULTIPLE selections. Both use SelectedValue to access the chosen item.

Back to Articles