Skip to main content

Blazor Switch

Use the Blazor Bootstrap Switch component to show the consistent cross-browser and cross-device custom checkboxes.

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

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" ValueExpression="() => agree" 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