ASP.NET - List Control - Dropdown

The DropDownList control is used to create a list of selectable items, where the user can select one option from the list. It is similar to the ListBox control, but with a different user interface that takes up less screen space.

Properties:

  • DataSource: Specifies the data source for the control.
  • DataTextField: Specifies the field from the data source to use as the text for the list items.
  • DataValueField: Specifies the field from the data source to use as the value for the list items.
  • SelectedValue: Gets or sets the selected value of the control.
  • SelectedIndex: Gets or sets the index of the selected item in the list.
  • Enabled: Gets or sets a value indicating whether the control is enabled or disabled.
  • AppendDataBoundItems: Gets or sets a value indicating whether items from the data source will be appended to the control.

Events:

  • SelectedIndexChanged: Occurs when the selected item in the control is changed.

Methods:

  • Items.Add: Adds an item to the list.
  • Items.Clear: Removes all items from the list.
  • Items.FindByValue: Finds an item in the list by its value.
  • Items.FindByText: Finds an item in the list by its text.

Example

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="WebApplication1.Default" %>
<!DOCTYPE html>
<html xmlns="www.w3.org;
<head runat="server">
    <title>DropDownList Example</title>
</head>
<body>
    <form id="form1" runat="server">
        <div>
            <asp:DropDownList ID="ddlFruits" runat="server"></asp:DropDownList>
            <asp:Button ID="btnSubmit" runat="server" Text="Submit" OnClick="btnSubmit_Click" />
        </div>
    </form>
</body>
</html>

In this example, we've included a DropDownList control with an ID of "ddlFruits". We haven't set the DataSourceID property in this example, because we're going to manually add items to the control in the code-behind file.

Here's the code for the Page_Load event handler in the code-behind file (Default.aspx.cs):

protected void Page_Load(object sender, EventArgs e)
{
    if (!IsPostBack)
    {
        ddlFruits.Items.Add("Apple");
        ddlFruits.Items.Add("Banana");
        ddlFruits.Items.Add("Orange");
    }
}
protected void btnSubmit_Click(object sender, EventArgs e)
{
    string selectedFruit = ddlFruits.SelectedItem.Text;
    Response.Write("You selected " + selectedFruit);
}

In this code, we've added items to the DropDownList control in the Page_Load event handler using the Items.Add method. We've also included a check for the IsPostBack property to ensure that the items are only added the first time the page is loaded.

We've also included the btnSubmit_Click event handler, which retrieves the selected item from the DropDownList control and writes a message to the response that displays the text of the selected item.

When you run this code, you should see a page with a DropDownList control containing the items "Apple", "Banana", and "Orange". When you select an item and click the Submit button, you should see a message that displays the selected item.