RadioButtonList¶
The RadioButtonList component renders a group of radio buttons that allow users to select a single item from a list. This component supports both static items and data-bound scenarios with various layout options.
Original Web Forms documentation: https://docs.microsoft.com/en-us/dotnet/api/system.web.ui.webcontrols.radiobuttonlist?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
- RepeatLayout options: Table, Flow, OrderedList, UnorderedList
- RepeatDirection options: Horizontal, Vertical
- TextAlign for label positioning (Left or Right)
- Disabled state via
Enabledparameter - Per-item disabled state via
ListItem.Enabled - Style attributes (BackColor, ForeColor, Font, etc.) and CssClass formatting
- CellPadding and CellSpacing for table layout
- 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 - RepeatColumns is not fully implemented
WebForms Syntax¶
<asp:RadioButtonList
ID="rbl1"
runat="server"
RepeatDirection="Vertical|Horizontal"
RepeatLayout="Table|Flow|OrderedList|UnorderedList"
TextAlign="Left|Right"
CellPadding="integer"
CellSpacing="integer"
CssClass="string"
Enabled="True|False"
Visible="True|False"
SelectedValue="string"
SelectedIndex="integer"
AutoPostBack="True|False"
OnSelectedIndexChanged="SelectedIndexChanged event handler"
DataTextField="string"
DataValueField="string">
<asp:ListItem Value="S" Text="Small" />
<asp:ListItem Value="M" Text="Medium" Selected="True" />
<asp:ListItem Value="L" Text="Large" />
</asp:RadioButtonList>
Rendered HTML (Table layout, Vertical direction):
<table id="rbl1">
<tr>
<td><input id="rbl1_0" type="radio" name="rbl1" value="S" /><label for="rbl1_0">Small</label></td>
</tr>
<tr>
<td><input id="rbl1_1" type="radio" name="rbl1" value="M" checked="checked" /><label for="rbl1_1">Medium</label></td>
</tr>
<tr>
<td><input id="rbl1_2" type="radio" name="rbl1" value="L" /><label for="rbl1_2">Large</label></td>
</tr>
</table>
Blazor Syntax¶
Static Items¶
<RadioButtonList TItem="object" StaticItems="items" @bind-SelectedValue="selectedSize" />
@code {
private string selectedSize = "M";
private ListItemCollection items = new()
{
new ListItem("Small", "S"),
new ListItem("Medium", "M"),
new ListItem("Large", "L")
};
}
Data Binding¶
<RadioButtonList TItem="Size"
Items="sizes"
DataTextField="Name"
DataValueField="Code"
@bind-SelectedValue="selectedSizeCode" />
@code {
private string selectedSizeCode = "";
private List<Size> sizes = new()
{
new Size { Code = "S", Name = "Small" },
new Size { Code = "M", Name = "Medium" },
new Size { Code = "L", Name = "Large" },
new Size { Code = "XL", Name = "Extra Large" }
};
public class Size
{
public string Code { get; set; }
public string Name { get; set; }
}
}
With Event Handler¶
<RadioButtonList TItem="object"
StaticItems="items"
@bind-SelectedValue="selectedValue"
OnSelectedIndexChanged="HandleSelectionChanged" />
@code {
private string selectedValue = "";
private void HandleSelectionChanged(ChangeEventArgs e)
{
Console.WriteLine($"Selection changed to: {selectedValue}");
}
}
Different Layouts¶
@* Flow layout - inline radio buttons *@
<RadioButtonList TItem="object"
StaticItems="items"
RepeatLayout="RepeatLayout.Flow"
RepeatDirection="RepeatDirection.Horizontal" />
@* Unordered list layout *@
<RadioButtonList TItem="object"
StaticItems="items"
RepeatLayout="RepeatLayout.UnorderedList" />
@* Ordered list layout *@
<RadioButtonList TItem="object"
StaticItems="items"
RepeatLayout="RepeatLayout.OrderedList" />
Label Position¶
@* Label before radio button (left-aligned text) *@
<RadioButtonList TItem="object"
StaticItems="items"
TextAlign="TextAlign.Left" />
@* Label after radio button (right-aligned text, default) *@
<RadioButtonList TItem="object"
StaticItems="items"
TextAlign="TextAlign.Right" />
With Styling¶
<RadioButtonList TItem="object"
StaticItems="items"
CssClass="custom-radio-list"
BackColor="new WebColor(System.Drawing.Color.LightGray)"
CellPadding="5"
CellSpacing="2" />
Key Differences from Web Forms¶
- Type Parameter: Blazor RadioButtonList 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
- Mutual Exclusion: All radio buttons automatically share the same
nameattribute for proper mutual exclusion
Key Difference from CheckBoxList¶
RadioButtonList renders radio buttons with a shared name attribute for mutual exclusion (only one can be selected), while CheckBoxList uses checkboxes with unique names allowing multiple selections:
<!-- RadioButtonList - all inputs have same name for mutual exclusion -->
<input type="radio" name="rbl1" value="S" />
<input type="radio" name="rbl1" value="M" checked />
<input type="radio" name="rbl1" value="L" />
<!-- CheckBoxList - each input has unique name -->
<input type="checkbox" name="cbl1$0" value="1" />
<input type="checkbox" name="cbl1$1" value="2" />
<input type="checkbox" name="cbl1$2" value="3" />
Common Patterns¶
Pre-select an Item¶
Disabled RadioButtonList¶
Disable Individual Items¶
@code {
private ListItemCollection items = new()
{
new ListItem("Available", "1") { Enabled = true },
new ListItem("Unavailable", "2") { Enabled = false },
new ListItem("Available", "3") { Enabled = true }
};
}
Get Selected Item Details¶
var radioList = // reference to RadioButtonList component
var selectedItem = radioList.SelectedItem;
var text = selectedItem?.Text;
var value = selectedItem?.Value;
Migration Tips¶
When migrating from Web Forms:
- Replace
<asp:RadioButtonList>with<RadioButtonList 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 - Update
RepeatLayoutandRepeatDirectionto use the enum syntax (e.g.,RepeatLayout.Table)
See Also¶
- RadioButton - Individual radio button control
- DropDownList - Single selection dropdown
- CheckBox - Individual checkbox control