Events
This article explains the events available in the Telerik DateInput for Blazor:
OnChange
The OnChange
event represents a user action - confirmation of the current value. It fires when the user presses Enter
in the input, or when the input loses focus.
The date input is a generic component, so you must provide either a Value
, or a type to the T
parameter of the component.
Handle OnChange
@result
<br />
<TelerikDateInput T="DateTime" OnChange="@MyOnChangeHandler"></TelerikDateInput>
@code {
string result;
private void MyOnChangeHandler(object theUserInput)
{
// the handler receives an object that you may need to cast to the type of the component
// 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)theUserInput);
}
}
The event is an
EventCallback
. It can be synchronous and returnvoid
, or asynchronous and returnasync Task
. Do not useasync void
.
The
OnChange
event is a custom event and does not interfere with bindings, so you can use it together with models and forms.
Handle OnChange and use two-way binding
@result
<br />
model value: @theInputValue
<br />
<TelerikDateInput @bind-Value="@theInputValue" OnChange="@MyOnChangeHandler"></TelerikDateInput>
@code {
string result;
DateTime? theInputValue { get; set; } = DateTime.Now;
private void MyOnChangeHandler(object theUserInput)
{
// the handler receives an object that you may need to cast to the type of the component
// 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}", (theUserInput as DateTime?).Value);
}
}
ValueChanged
The ValueChanged
event fires upon every change (for example, keystroke) in the input.
Handle ValueChanged
@result
<br />
<TelerikDateInput ValueChanged="@( (DateTime d) => MyValueChangeHandler(d) )"></TelerikDateInput>
@code {
string result;
private void MyValueChangeHandler(DateTime theUserInput)
{
result = string.Format("The user entered: {0}", theUserInput);
}
}
The event is an
EventCallback
. It can be synchronous and returnvoid
, or asynchronous and returnasync Task
. Do not useasync void
.
The lambda expression in the handler is required by the framework: https://github.com/aspnet/AspNetCore/issues/12226.
Handle ValueChanged and provide initial value
@result
<br />
model value: @theInputValue
<br />
<TelerikDateInput Value="@theInputValue" ValueChanged="@( (DateTime d) => MyValueChangeHandler(d) )"></TelerikDateInput>
@code {
string result;
DateTime theInputValue { get; set; } = DateTime.Now;
private void MyValueChangeHandler(DateTime theUserInput)
{
result = string.Format("The user entered: {0:dd/MMM/yyyy}", theUserInput);
//you have to update the model manually because handling the ValueChanged event does not let you use @bind-Value
theInputValue = theUserInput;
}
}
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 *@
<TelerikDateInput @bind-Value="@TheDate"
OnBlur="@OnBlurHandler">
</TelerikDateInput>
@code{
async Task OnBlurHandler()
{
Console.WriteLine($"BLUR fired, current value is {TheDate}.");
}
DateTime? TheDate { get; set; } = DateTime.Now;
}