|
| 1 | +--- |
| 2 | +title: Events |
| 3 | +page_title: DatePicker for Blazor | Events |
| 4 | +description: Events in the DatePicker for Blazor |
| 5 | +slug: components/datepicker/events |
| 6 | +tags: telerik,blazor,DatePicker,events |
| 7 | +published: true |
| 8 | +position: 20 |
| 9 | +--- |
| 10 | + |
| 11 | +# Events |
| 12 | + |
| 13 | +This article explsins the events available in the Telerik DatePicker for Blazor: |
| 14 | + |
| 15 | +* [OnChange](#onchange) |
| 16 | +* [ValueChanged](#valuechanged) |
| 17 | + |
| 18 | +## OnChange |
| 19 | + |
| 20 | +The `OnChange` event fires when the new value is commited by the user either by pressing `Enter`, or when the input loses focus. |
| 21 | + |
| 22 | +The date picker is a generic component, so you must provide either a `Value`, or a type to the `T` parameter of the component. |
| 23 | + |
| 24 | +>caption Handle OnChange |
| 25 | +
|
| 26 | +````CSHTML |
| 27 | +@using Telerik.Blazor.Components.DatePicker |
| 28 | +
|
| 29 | +<TelerikDatePicker T="DateTime" OnChange="@MyOnChangeHandler"></TelerikDatePicker> |
| 30 | +
|
| 31 | +<br /> |
| 32 | +@result |
| 33 | +
|
| 34 | +@code { |
| 35 | + string result; |
| 36 | +
|
| 37 | + private void MyOnChangeHandler(object theUserInput) |
| 38 | + { |
| 39 | + // the handler receives an object that you may need to cast to the type of the component |
| 40 | + // if you do not provide a Value, you must provide the Type parameter to the component |
| 41 | + result = string.Format("The user entered: {0:dd/MMM/yyyy}", (DateTime)theUserInput); |
| 42 | + } |
| 43 | +} |
| 44 | +```` |
| 45 | + |
| 46 | +>tip The `OnChange` event is a custom event and does not interfere with bindings, so you can use it together with models and forms. |
| 47 | +
|
| 48 | +>caption Handle OnChange and use two-way binding |
| 49 | +
|
| 50 | +````CSHTML |
| 51 | +@using Telerik.Blazor.Components.DatePicker |
| 52 | +
|
| 53 | +<TelerikDatePicker @bind-Value="@thePickerValue" OnChange="@MyOnChangeHandler"></TelerikDatePicker> |
| 54 | +
|
| 55 | +<br /> |
| 56 | +@result |
| 57 | +<br /> |
| 58 | +model value: @thePickerValue |
| 59 | +
|
| 60 | +@code { |
| 61 | + string result; |
| 62 | +
|
| 63 | + DateTime? thePickerValue { get; set; } = DateTime.Now; |
| 64 | +
|
| 65 | + private void MyOnChangeHandler(object theUserInput) |
| 66 | + { |
| 67 | + // the handler receives an object that you may need to cast to the type of the component |
| 68 | + // if you do not provide a Value, you must provide the Type parameter to the component |
| 69 | + result = string.Format("The user entered: {0:dd/MMM/yyyy}", (theUserInput as DateTime?).Value); |
| 70 | + } |
| 71 | +} |
| 72 | +```` |
| 73 | + |
| 74 | +## ValueChanged |
| 75 | + |
| 76 | +The `ValueChanged` event fires upon every change (for example, keystroke) in the input. |
| 77 | + |
| 78 | +>caption Handle ValueChanged |
| 79 | +
|
| 80 | +````CSHTML |
| 81 | +@using Telerik.Blazor.Components.DatePicker |
| 82 | +
|
| 83 | +<TelerikDatePicker ValueChanged="@( (DateTime d) => MyValueChangeHandler(d) )"></TelerikDatePicker> |
| 84 | +
|
| 85 | +<br /> |
| 86 | +@result |
| 87 | +
|
| 88 | +@code { |
| 89 | + string result; |
| 90 | +
|
| 91 | + private void MyValueChangeHandler(DateTime theUserInput) |
| 92 | + { |
| 93 | + result = string.Format("The user entered: {0}", theUserInput); |
| 94 | + } |
| 95 | +} |
| 96 | +```` |
| 97 | + |
| 98 | +@[template](/_contentTemplates/common/issues-and-warnings.md#valuechanged-lambda-required) |
| 99 | + |
| 100 | +>caption Handle ValueChanged and provide initial value |
| 101 | +
|
| 102 | +````CSHTML |
| 103 | +@using Telerik.Blazor.Components.DatePicker |
| 104 | +
|
| 105 | +<TelerikDatePicker Value="@thePickerValue" ValueChanged="@( (DateTime d) => MyValueChangeHandler(d) )"></TelerikDatePicker> |
| 106 | +
|
| 107 | +<br /> |
| 108 | +@result |
| 109 | +<br /> |
| 110 | +model value: @thePickerValue |
| 111 | +
|
| 112 | +@code { |
| 113 | + string result; |
| 114 | +
|
| 115 | + DateTime thePickerValue { get; set; } = DateTime.Now; |
| 116 | +
|
| 117 | + private void MyValueChangeHandler(DateTime theUserInput) |
| 118 | + { |
| 119 | + result = string.Format("The user entered: {0:dd/MMM/yyyy}", theUserInput); |
| 120 | +
|
| 121 | + //you have to update the model manually because handling the ValueChanged event does not let you use @bind-Value |
| 122 | + thePickerValue = theUserInput; |
| 123 | + } |
| 124 | +} |
| 125 | +```` |
| 126 | + |
| 127 | +## See Also |
| 128 | + |
| 129 | +* [ValueChanged and Validation]({%slug value-changed-validation-model%}) |
0 commit comments