DropDownList¶
The DropDownList component renders an HTML <select> element that allows users to select a single item from a drop-down list. This component supports both static items and data-bound scenarios.
Original Web Forms documentation: https://docs.microsoft.com/en-us/dotnet/api/system.web.ui.webcontrols.dropdownlist?view=netframework-4.8
Blazor Features Supported¶
- Static items via
StaticItemsparameter withListItemcollection - Data binding via
Itemsparameter withDataTextFieldandDataValueField - Two-way binding with
@bind-SelectedValue - Selected item tracking via
SelectedValueandSelectedIndex - OnSelectedIndexChanged event handler
- Disabled state via
Enabledparameter - Style attributes (BackColor, ForeColor, Font, etc.) and CssClass formatting
- Access to
SelectedItemproperty
WebForms Features Not Supported¶
- AutoPostBack is not supported in Blazor - use
OnSelectedIndexChangedevent instead - AppendDataBoundItems is not implemented
- DataSourceID is not supported - bind directly to collections via
Itemsparameter
WebForms Syntax¶
<asp:DropDownList
AccessKey="string"
AutoPostBack="True|False"
BackColor="color name|#dddddd"
BorderColor="color name|#dddddd"
BorderStyle="NotSet|None|Dotted|Dashed|Solid|Double|Groove|Ridge|Inset|Outset"
BorderWidth="size"
CssClass="string"
DataSourceID="string"
DataTextField="string"
DataValueField="string"
Enabled="True|False"
EnableTheming="True|False"
EnableViewState="True|False"
Font-Bold="True|False"
Font-Italic="True|False"
Font-Names="string"
Font-Overline="True|False"
Font-Size="string|Smaller|Larger|XX-Small|X-Small|Small|Medium|Large|X-Large|XX-Large"
Font-Strikeout="True|False"
Font-Underline="True|False"
ForeColor="color name|#dddddd"
Height="size"
ID="string"
OnDataBinding="DataBinding event handler"
OnDisposed="Disposed event handler"
OnInit="Init event handler"
OnLoad="Load event handler"
OnPreRender="PreRender event handler"
OnSelectedIndexChanged="SelectedIndexChanged event handler"
OnUnload="Unload event handler"
runat="server"
SelectedIndex="number"
SelectedValue="string"
TabIndex="integer"
ToolTip="string"
Visible="True|False"
Width="size">
<asp:ListItem Value="value1" Text="Display Text 1" Selected="True|False" />
<asp:ListItem Value="value2" Text="Display Text 2" />
</asp:DropDownList>
Blazor Syntax¶
Static Items¶
<DropDownList TItem="object" StaticItems="items" @bind-SelectedValue="selectedValue" />
@code {
private string selectedValue = "";
private ListItemCollection items = new()
{
new ListItem("Select...", ""),
new ListItem("Option One", "1"),
new ListItem("Option Two", "2"),
new ListItem("Option Three", "3")
};
}
Data Binding¶
<DropDownList TItem="Product"
Items="products"
DataTextField="Name"
DataValueField="Id"
@bind-SelectedValue="selectedProductId" />
@code {
private string selectedProductId = "";
private List<Product> products = new()
{
new Product { Id = "1", Name = "Widget" },
new Product { Id = "2", Name = "Gadget" },
new Product { Id = "3", Name = "Gizmo" }
};
public class Product
{
public string Id { get; set; }
public string Name { get; set; }
}
}
With Event Handler¶
<DropDownList TItem="object"
StaticItems="items"
@bind-SelectedValue="selectedValue"
OnSelectedIndexChanged="HandleSelectionChanged" />
@code {
private string selectedValue = "";
private void HandleSelectionChanged(ChangeEventArgs e)
{
var newValue = e.Value?.ToString();
Console.WriteLine($"Selection changed to: {newValue}");
}
}
With Styling¶
<DropDownList TItem="object"
StaticItems="items"
CssClass="form-select"
BackColor="new WebColor(System.Drawing.Color.LightYellow)"
Font="new FontInfo { Bold = true }" />
Key Differences from Web Forms¶
- Type Parameter: Blazor DropDownList requires a
TItemtype parameter for data binding - Property Names: Use
StaticItemsfor the item collection (notItems), asItemsis reserved for data-bound scenarios - Two-way Binding: Use
@bind-SelectedValuefor automatic two-way binding - Events: Use
OnSelectedIndexChangedwithChangeEventArgsinstead of specialized event args - No AutoPostBack: Blazor's event model doesn't require postback; events fire immediately
ListItem and ListItemCollection¶
The ListItem class represents individual items in the dropdown:
public class ListItem
{
public string Text { get; set; }
public string Value { get; set; }
public bool Selected { get; set; }
public bool Enabled { get; set; } = true;
public ListItem(string text)
public ListItem(string text, string value)
public ListItem(string text, string value, bool selected)
}
The ListItemCollection class provides helper methods:
public class ListItemCollection : List<ListItem>
{
public ListItem FindByValue(string value)
public ListItem FindByText(string text)
}
Common Patterns¶
Pre-select an Item¶
Disabled DropDownList¶
Get Selected Item Details¶
var dropdown = // reference to DropDownList component
var selectedItem = dropdown.SelectedItem;
var text = selectedItem?.Text;
var value = selectedItem?.Value;
Migration Tips¶
When migrating from Web Forms:
- Replace
<asp:DropDownList>with<DropDownList TItem="object"> - Rename any
Itemsparameter toStaticItems - Replace
AutoPostBack="true"withOnSelectedIndexChangedevent handler - Use
@bind-SelectedValueinstead of manually managingSelectedValue - Remove
runat="server"attribute - Remove
<asp:ListItem>tags and define items in code-behind asListItemCollection
See Also¶
- ListBox - Multiple selection list control
- CheckBoxList - Multiple checkboxes in a list
- RadioButtonList - Radio button selection list