Skip to main content

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.

Blazor Bootstrap: Switch Component

Parameters​

NameTypeDefaultRequiredDescriptionAdded Version
DisabledboolfalseGets or sets the disabled state.1.3.0
LabelstringnullGets or sets the label.1.3.0
ReverseboolfalseDetermines whether to put the switch on the opposite side.1.3.0
ValueboolfalseGets or sets the value.1.3.0

Methods​

NameReturn TypeDescriptionAdded Version
Disable()voidDisables switch.1.3.0
Enable()voidEnables switch.1.3.0

Events​

NameDescriptionAdded Version
ValueChangedThis event fired when the switch selection changed.1.3.0

Examples​

Basic usage​

Blazor Bootstrap: Switch Component - 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;
}

See demo here

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.

NOTE

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();
}

See demo here

Reverse​

Put your switches on the opposite side by using the Reverse parameter.

Blazor Bootstrap: Switch Component - Reverse
<Switch @bind-Value="agree" Label="Reverse switch checkbox input" Reverse="true" />
@code {
bool agree;
}

See demo here

Events: ValueChanged​

This event fired when the Switch selection changed.

Blazor Bootstrap: Switch Component - Events: ValueChanged
<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()}.";
}
}

See demo here

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);
}

See demo here

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;
}
}

See demo here

More libraries from the team

Free, open-source .NET libraries built by the same team.

UnlimitedPDF

A pure .NET library to create, merge and split PDFs, and convert HTML to PDF.

BlazorExpress.ChartJS

Chart.js charts for Blazor, configured in C#.

BlazorExpress.Bulma

Enterprise-class Blazor components built on the Bulma CSS framework.