Create consistent cross-browser and cross-device checkboxes with our blazor switches. A switch has the markup of a custom checkbox. The examples below cover the disabled state, a reversed layout, the ValueChanged event, a read-only value and use in a form.
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.
@using System.ComponentModel.DataAnnotations
<EditForm EditContext="@editContext" OnValidSubmit="HandleOnValidSubmit">
<DataAnnotationsValidator />
<div class="form-group row mb-3">
<label class="col-md-2 col-form-label">First name: <span class="text-danger">*</span></label>
<div class="col-md-10">
<InputText class="form-control" @bind-Value="employee.FirstName" Placeholder="Enter first name" />
<ValidationMessage For="@(() => employee.FirstName)" />
</div>
</div>
<div class="form-group row mb-3">
<label class="col-md-2 col-form-label">Last name: <span class="text-danger">*</span></label>
<div class="col-md-10">
<InputText class="form-control" @bind-Value="employee.LastName" Placeholder="Enter last name" />
<ValidationMessage For="@(() => employee.LastName)" />
</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" @bind-Value="employee.IsActive" />
</div>
</div>
<div class="row">
<div class="col-md-12 text-right">
<Button Type="ButtonType.Button" Color="ButtonColor.Secondary" Class="float-end" @onclick="ResetForm">Reset</Button>
<Button Type="ButtonType.Submit" Color="ButtonColor.Success" Class="float-end me-2" Disabled="disableSave">Save</Button>
</div>
</div>
</EditForm>
@code {
private bool disableSave;
private Employee employee = new();
private EditContext? editContext;
[Inject] private ToastService ToastService { get; set; } = default!;
protected override void OnInitialized()
{
employee = new() { FirstName = "Elena", LastName = "Novak", IsActive = true };
editContext = new EditContext(employee);
editContext.OnFieldChanged += HandleFieldChanged!;
base.OnInitialized();
}
public async Task HandleOnValidSubmit()
{
// .NET 10 and earlier: use editContext.Validate()
disableSave = !(editContext is not null && await editContext.ValidateAsync());
var toastMessage = new ToastMessage
{
Title = "Save Employee Details",
Message = $"Employee details saved successfully.",
AutoHide = true,
Type = ToastType.Success,
IconName = IconName.CheckCircleFill,
};
ToastService.Notify(toastMessage);
}
private async void HandleFieldChanged(object sender, FieldChangedEventArgs e)
{
// .NET 10 and earlier: use editContext.Validate()
disableSave = !(editContext is not null && await editContext.ValidateAsync());
StateHasChanged();
var toastMessage = new ToastMessage
{
Title = "Field Changed Notification",
Message = $"The field \"{e.FieldIdentifier.FieldName}\" was changed.",
AutoHide = true,
Type = ToastType.Info
};
ToastService.Notify(toastMessage);
}
private void ResetForm()
{
employee = new();
editContext = new EditContext(employee);
editContext.OnFieldChanged += HandleFieldChanged!;
}
public class Employee
{
[Required(ErrorMessage = "First name required.")]
public string? FirstName { get; set; }
[Required(ErrorMessage = "Last name required.")]
public string? LastName { get; set; }
public bool IsActive { get; set; }
}
}
The Switch can also 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.