Blazor Switch
Use the Blazor Bootstrap Switch component for consistent cross-browser and cross-device toggle switches, with a disabled state, a reversed layout, a read-only value, a ValueChanged event and use in forms.
Parameters
| Name | Type | Default | Required | Description | Added Version |
|---|---|---|---|---|---|
| Disabled | bool | false | Gets or sets the disabled state. | 1.3.0 | |
| Label | string | null | Gets or sets the label. | 1.3.0 | |
| Reverse | bool | false | Determines whether to put the switch on the opposite side. | 1.3.0 | |
| Value | bool | false | Gets or sets the value. | 1.3.0 |
Methods
| Name | Return Type | Description | Added Version |
|---|---|---|---|
| Disable() | void | Disables switch. | 1.3.0 |
| Enable() | void | Enables switch. | 1.3.0 |
Events
| Name | Description | Added Version |
|---|---|---|
| ValueChanged | This event fired when the switch selection changed. | 1.3.0 |
Examples
Basic usage
<Switch @bind-Value="agree1" Label="Default switch checkbox input" />
<Switch @bind-Value="agree2" Label="Checked switch checkbox input" />
<div class="mt-3">Switch 1 Status: <b>@agree1</b></div>
<div>Switch 2 Status: <b>@agree2</b></div>
@code {
bool agree1;
bool agree2 = true;
}
Disable
Use the Disabled parameter to disable the Switch.
<div class="mb-3">
<Switch @bind-Value="agree" Disabled="@disabled" Label="Disabled switch checkbox input" />
</div>
<Button Color="ButtonColor.Primary" @onclick="Enable"> Enable </Button>
<Button Color="ButtonColor.Secondary" @onclick="Disable"> Disable </Button>
<Button Color="ButtonColor.Warning" @onclick="Toggle"> Toggle </Button>
@code {
private bool agree = true;
private bool disabled = true;
private void Enable() => disabled = false;
private void Disable() => disabled = true;
private void Toggle() => disabled = !disabled;
}
Also, use Enable() and Disable() methods to enable and disable the Switch.
Do not use both the Disabled parameter and Enable() & Disable() methods.
<div class="mb-3">
<Switch @ref="switch1" @bind-Value="agree" Label="Disabled switch checkbox input" />
</div>
<Button Color="ButtonColor.Secondary" @onclick="Disable"> Disable </Button>
<Button Color="ButtonColor.Primary" @onclick="Enable"> Enable </Button>
@code {
private Switch switch1 = default!;
private bool agree = true;
private void Disable() => switch1.Disable();
private void Enable() => switch1.Enable();
}
Reverse
Put your switches on the opposite side by using the Reverse parameter.
<Switch @bind-Value="agree" Label="Reverse switch checkbox input" Reverse="true" />
@code {
bool agree;
}
Events: ValueChanged
This event fired when the Switch selection changed.
<Switch Value="agree" Label="Default switch checkbox input" ValueChanged="SwitchChanged" />
<div class="mt-3">@displaySwitchStatus</div>
@code {
private bool agree;
private string displaySwitchStatus;
private void SwitchChanged(bool value)
{
agree = value; // this is mandatory
displaySwitchStatus = $"Switch Status: {value}, changed at {DateTime.Now.ToLocalTime()}.";
}
}
Read-only value
Set only the Value parameter to display a value that cannot be written back, such as a read-only property. Combine it with Disabled so users cannot toggle the display.
<table class="table">
<thead>
<tr>
<th>Name</th>
<th>Active</th>
</tr>
</thead>
<tbody>
@foreach (var employee in employees)
{
<tr>
<td>@employee.Name</td>
<td><Switch Value="employee.IsActive" Disabled="true" /></td>
</tr>
}
</tbody>
</table>
@code {
private readonly List<Employee> employees = new()
{
new("Elena Novak", true),
new("Ada Lovelace", false),
new("Grace Hopper", true)
};
private record Employee(string Name, bool IsActive);
}
Form
The Switch can be used inside an EditForm with Value and ValueChanged instead of @bind-Value. Use @bind-Value when you want the EditContext to track the field.
<EditForm Model="@employee" OnValidSubmit="HandleOnValidSubmit">
<DataAnnotationsValidator />
<div class="form-group row mb-3">
<label class="col-md-2 col-form-label">Name: <span class="text-danger">*</span></label>
<div class="col-md-10">
<TextInput @bind-Value="@employee.Name" Placeholder="Enter name" />
<ValidationMessage For="@(() => employee.Name)" />
</div>
</div>
<div class="form-group row mb-3">
<label class="col-md-2 col-form-label">Active</label>
<div class="col-md-10">
<Switch Class="mt-2" Value="@employee.IsActive" ValueChanged="OnIsActiveChanged" />
</div>
</div>
<div class="row">
<div class="col-md-12">
<Button Type="ButtonType.Submit" Color="ButtonColor.Success" Class="float-end">Submit</Button>
</div>
</div>
</EditForm>
<div class="mt-3">@submitMessage</div>
@using System.ComponentModel.DataAnnotations
@code {
private Employee employee = new();
private string? submitMessage;
private void OnIsActiveChanged(bool value)
{
employee.IsActive = value;
submitMessage = null;
}
private void HandleOnValidSubmit()
{
submitMessage = $"Submitted: {employee.Name} (Active: {employee.IsActive})";
}
public class Employee
{
[Required(ErrorMessage = "Name required.")]
public string? Name { get; set; }
public bool IsActive { get; set; } = true;
}
}