New to Telerik UI for Blazor? Download free 30-day trial

Events

This article describes the events of the Telerik DatePicker for Blazor.

OnBlur

The OnBlur event fires when the component loses focus.

Handle the OnBlur event

@* You do not have to use OnChange to react to loss of focus *@

@result

<TelerikDatePicker @bind-Value="@DatePickerValue"
                   OnBlur="@OnBlurHandler">
</TelerikDatePicker>

@code {
    private string result = string.Empty;

    private DateTime? DatePickerValue { get; set; } = DateTime.Today;

    private void OnBlurHandler()
    {
        result = string.Format("BLUR fired, current value is {0:dd/MMM/yyyy}", DatePickerValue);
    }
}

The event is an EventCallback. It can be synchronous and return void, or asynchronous and return async Task. Do not use async void.

OnCalendarCellRender

The OnCalendarCellRender event fires when each calendar cell in each view is about to render. The event allows you to:

  • Identify the current view.
  • Find out the cell date.
  • Set a custom CSS class for the <td> element.

As an argument, the event handler receives a DatePickerCalendarCellRenderEventArgs object, which contains the following properties:

Property Type Description
Class string A custom CSS class for the calendar cell DOM element.
Date DateTime The date of the calendar cell.
View CalendarView enum
(Month)
The currently visible view. You can use it to determine if the calendar is rendering the MonthView, YearView, and so on.

Handle the OnCalendarCellRender event.

@* Customize the calendar cells using the OnCalendarCellRender event. *@

<TelerikDatePicker @bind-Value="@DatePickerValue"
                   OnCalendarCellRender="@OnCalendarCellRenderHandler"
                   Width="295px">
</TelerikDatePicker>

<style>
    .special {
        color: white;
        background-color: greenyellow;
        font-weight: bold;
    }
</style>

@code {
    private DateTime? DatePickerValue { get; set; } = DateTime.Today;

    private void OnCalendarCellRenderHandler(DatePickerCalendarCellRenderEventArgs args)
    {
        if (args.View == CalendarView.Month)
        {
            args.Class = args.Date.Day % 3 == 0 ? "special" : "";
        }
        else if (args.View == CalendarView.Decade)
        {
            args.Class = args.Date.Year == 2020 ? "special" : "";
        }
    }
}

OnChange

The OnChange event represents a user action that confirms the current value. It fires when the user presses Enter in the input or when the input loses focus.

The event handler receives an object argument that you need to cast to the actual Value type. The argument can hold a value or be null, depending on the user input and the Value type.

The DatePicker is a generic component, so you must either provide a Value, or a type to the T parameter of the component.

Handle OnChange and use two-way binding

@result
<br />
model value: @DatePickerValue
<br />

<TelerikDatePicker @bind-Value="@DatePickerValue"
                   OnChange="@MyOnChangeHandler">
</TelerikDatePicker>

@code {
    private string result = string.Empty;

    private DateTime? DatePickerValue { get; set; } = DateTime.Today;

    private void MyOnChangeHandler(object userInput)
    {
        // if you do not provide a Value, you must provide the Type parameter to the component
        result = string.Format("The user entered: {0:dd/MMM/yyyy}", (DateTime)userInput);
    }
}

The event is an EventCallback. It can be synchronous and return void, or asynchronous and return async Task. Do not use async void.

The OnChange event is a custom event and does not interfere with bindings, so you can use it together with models and forms.

OnClose

The OnClose event fires before the DatePicker popup closes.

The event handler receives as an argument an DatePickerCloseEventArgs object that contains:

Property Description
IsCancelled Set the IsCancelled property to true to cancel the closing of the popup.
@* Cancel the OnClose event based on a condition *@

<TelerikDatePicker @bind-Value="@DatePickerValue"
                   OnClose="@OnDatePickerPopupClose">
</TelerikDatePicker>

@code {
    private DateTime? DatePickerValue { get; set; } = DateTime.Today;

    private void OnDatePickerPopupClose(DatePickerCloseEventArgs args)
    {
        //cancel the OnClose event based on a condition
        if (DatePickerValue > DateTime.Today)
        {
            args.IsCancelled = true;
        }
    }
}

The event is an EventCallback. It can be synchronous and return void, or asynchronous and return async Task. Do not use async void.

OnOpen

The OnOpen event fires before the DatePicker popup renders.

As an argument, the event handler receives a DatePickerOpenEventArgs object, which contains the following properties:

Property Description
IsCancelled Set the IsCancelled property to true to cancel the opening of the popup.
<TelerikDatePicker @bind-Value="@DatePickerValue"
                   OnOpen="@OnDatePickerPopupOpen">
</TelerikDatePicker>

@code {
    private DateTime? DatePickerValue { get; set; } = DateTime.Today;

    private void OnDatePickerPopupOpen(DatePickerOpenEventArgs args)
    {
        //set the IsCancelled to true to cancel the OnOpen event
        args.IsCancelled = false;
    }
}

The event is an EventCallback. It can be synchronous and return void, or asynchronous and return async Task. Do not use async void.

ValueChanged

The ValueChanged event fires:

  • On Calendar selection and during typing when the resulting input value is valid.
  • On input blur if the input value is not valid and the Value type is nullable.

Handle ValueChanged and provide initial value

@result
<br />
model value: @DatePickerValue
<br />

<TelerikDatePicker Value="@DatePickerValue" 
                   ValueChanged="@( (DateTime inputDate) => MyValueChangeHandler(inputDate) )">
</TelerikDatePicker>

@code {
    private string result = string.Empty;

    private DateTime DatePickerValue { get; set; } = DateTime.Today;

    private void MyValueChangeHandler(DateTime userInput)
    {
        result = string.Format("The user entered: {0:dd/MMM/yyyy}", userInput);

        //you have to update the model manually because handling the ValueChanged event does not let you use @bind-Value
        DatePickerValue = userInput;
    }
}

The event is an EventCallback. It can be synchronous and return void, or asynchronous and return async Task. Do not use async void.

The lambda expression in the handler is required by the framework: https://github.com/aspnet/AspNetCore/issues/12226.

See Also

In this article