aspnet
  1. aspnet-dropdownlist

DropDownList - (ASP.NET Web Forms)

A DropDownList is a form control in the ASP.NET Web Forms framework used to display a list of items from which the user can select a single option. The values in the list can be set declaratively or dynamically, and the control can be bound to a data source.

Syntax

The DropDownList control is declared using the <asp:DropDownList> tag in the .aspx markup file. Here's an example of the syntax for declaring a DropDownList control:

<asp:DropDownList ID="ddlExample" runat="server">
  <asp:ListItem Text="Option 1" Value="1"></asp:ListItem>
  <asp:ListItem Text="Option 2" Value="2"></asp:ListItem>
  <asp:ListItem Text="Option 3" Value="3"></asp:ListItem>
</asp:DropDownList>

By default, when the page is rendered, the first item in the list is selected. You can change this behavior by setting the SelectedIndex property, or by setting the SelectedValue property to the value of the item you want to be selected.

Example

Here's an example of declaring a DropDownList control in an .aspx file and populating it with options dynamically:

<asp:DropDownList ID="ddlColors" runat="server"></asp:DropDownList>
protected void Page_Load(object sender, EventArgs e)
{
    ddlColors.Items.Add(new ListItem("Red", "1"));
    ddlColors.Items.Add(new ListItem("Green", "2"));
    ddlColors.Items.Add(new ListItem("Blue", "3"));
}

Alternatively, you can use data binding to populate the DropDownList control from a data source.

<asp:DropDownList ID="ddlProducts" runat="server" DataSourceID="productDataSource" DataTextField="ProductName" DataValueField="ProductID"></asp:DropDownList>

<asp:SqlDataSource ID="productDataSource" runat="server" ConnectionString="<%$ ConnectionStrings:NorthwindConnectionString %>" SelectCommand="SELECT [ProductID], [ProductName] FROM [Products]"></asp:SqlDataSource>

In this example, the DropDownList control is bound to a SqlDataSource control, which retrieves a list of products from a database.

Output

The output of the DropDownList control depends on how it is configured and populated. When the user selects an item from the list, the SelectedIndexChanged event is fired, which you can handle to perform some action based on the selected option.

Explanation

The DropDownList control is a useful form control in the ASP.NET Web Forms framework that allows users to select a single option from a list of options. You can populate the list declaratively or dynamically, and the control can be bound to a data source for more flexibility.

Use

The DropDownList control is commonly used in web forms to allow users to select a single option from a set of options. It can be used to capture user input or to allow users to make selections that affect some other aspect of the page or application.

Important Points

  • The DropDownList control allows users to select a single option from a list of options.
  • You can populate the list declaratively or dynamically, or bind it to a data source.
  • When the user selects an option, the SelectedIndexChanged event is fired, which you can handle to perform some action based on the selected option.

Summary

In this page, we discussed the DropDownList control in the ASP.NET Web Forms framework. We covered the syntax for declaring and populating a DropDownList control, examples of dynamic and data-bound lists, output, explanation, use cases, and important points. The DropDownList control is a useful tool for capturing user input in web forms.

Published on: