|
| 1 | +--- |
| 2 | +title: Events |
| 3 | +page_title: AutoComplete for Blazor | Events |
| 4 | +description: Events in the AutoComplete for Blazor |
| 5 | +slug: autocomplete-events |
| 6 | +tags: telerik,blazor,autocomplete,events |
| 7 | +published: true |
| 8 | +position: 20 |
| 9 | +--- |
| 10 | + |
| 11 | +# AutoComplete Events |
| 12 | + |
| 13 | +This article explains the events available in the Telerik ComboBox for Blazor: |
| 14 | + |
| 15 | +* [ValueChanged](#valuechanged) |
| 16 | +* [OnChange](#onchange) |
| 17 | + |
| 18 | + |
| 19 | +## ValueChanged |
| 20 | + |
| 21 | +The `ValueChanged` event fires upon every change of the user selection. When [custom values]({%slug components/combobox/custom-value%}) are enabled, it fires upon every keystroke, like in a regular `<input>` element. |
| 22 | + |
| 23 | +The examples below use binding to primitive types for brevity, you can use [full models]({%slug components/combobox/databind%}) as well. Make sure to review the [Data Binding - Missing Value or Data]({%slug components/combobox/databind%}#missing-value-or-data) section to provide all necessary parameters to the component if you do so. The type of the argument in the lambda expression must match the type of the `Value` of the component. |
| 24 | + |
| 25 | +>caption Handle ValueChanged |
| 26 | +
|
| 27 | +````CSHTML |
| 28 | +@result |
| 29 | +<br /> |
| 30 | +<TelerikComboBox Data="@MyList" ValueChanged="@( (string v) => MyValueChangeHandler(v) )"> |
| 31 | +</TelerikComboBox> |
| 32 | +
|
| 33 | +@code { |
| 34 | + string result; |
| 35 | +
|
| 36 | + private void MyValueChangeHandler(string theUserChoice) |
| 37 | + { |
| 38 | + result = string.Format("The user chose: {0}", theUserChoice); |
| 39 | + } |
| 40 | +
|
| 41 | + protected List<string> MyList = new List<string>() { "first", "second", "third" }; |
| 42 | +} |
| 43 | +```` |
| 44 | + |
| 45 | +>caption Handle ValueChanged with custom values - the event fires on every keystroke |
| 46 | +
|
| 47 | +````CSHTML |
| 48 | +@result |
| 49 | +<br /> |
| 50 | +<TelerikComboBox Data="@MyList" AllowCustom="true" ValueChanged="@( (string v) => MyValueChangeHandler(v) )"> |
| 51 | +</TelerikComboBox> |
| 52 | +
|
| 53 | +@code { |
| 54 | + string result; |
| 55 | +
|
| 56 | + private void MyValueChangeHandler(string theUserChoice) |
| 57 | + { |
| 58 | + result = string.Format("The user chose: {0}", theUserChoice); |
| 59 | + } |
| 60 | +
|
| 61 | + protected List<string> MyList = new List<string>() { "first", "second", "third" }; |
| 62 | +} |
| 63 | +```` |
| 64 | + |
| 65 | +@[template](/_contentTemplates/common/general-info.md#event-callback-can-be-async) |
| 66 | + |
| 67 | +@[template](/_contentTemplates/common/issues-and-warnings.md#valuechanged-lambda-required) |
| 68 | + |
| 69 | +>caption Handle ValueChanged and provide initial value (it is *not* required to enable custom values) |
| 70 | +
|
| 71 | +````CSHTML |
| 72 | +@result |
| 73 | +<br /> |
| 74 | +from model: @MyItem |
| 75 | +<br /> |
| 76 | +<br /> |
| 77 | +<TelerikComboBox Data="@MyList" Value="@MyItem" AllowCustom="true" ValueChanged="@( (string v) => MyValueChangeHandler(v) )"> |
| 78 | +</TelerikComboBox> |
| 79 | +
|
| 80 | +@code { |
| 81 | + string result; |
| 82 | +
|
| 83 | + private void MyValueChangeHandler(string theUserChoice) |
| 84 | + { |
| 85 | + result = string.Format("The user chose: {0}", theUserChoice); |
| 86 | +
|
| 87 | + //you have to update the model manually because handling the ValueChanged event does not let you use @bind-Value |
| 88 | + MyItem = theUserChoice; |
| 89 | + } |
| 90 | +
|
| 91 | + protected List<string> MyList = new List<string>() { "first", "second", "third" }; |
| 92 | +
|
| 93 | + protected string MyItem { get; set; } = "second"; |
| 94 | +} |
| 95 | +```` |
| 96 | + |
| 97 | +>caption Get selected item - check if the new value is present in the data source |
| 98 | +
|
| 99 | +````CSHTML |
| 100 | +@result |
| 101 | +<br /> |
| 102 | +Is selection from dropdown: @isLastSelectionInData |
| 103 | +<br /> |
| 104 | +from model: @MyItem |
| 105 | +<br /> |
| 106 | +<br /> |
| 107 | +<TelerikComboBox Data="@MyList" Value="@MyItem" AllowCustom="true" ValueChanged="@( (string v) => MyValueChangeHandler(v) )"> |
| 108 | +</TelerikComboBox> |
| 109 | +
|
| 110 | +@code { |
| 111 | + string result; |
| 112 | + bool isLastSelectionInData { get; set; } = true;//default to true as the default in this sample is from the predefined data |
| 113 | +
|
| 114 | + private void MyValueChangeHandler(string theUserChoice) |
| 115 | + { |
| 116 | + isLastSelectionInData = MyList.Contains(theUserChoice); //adapt to your actual data source |
| 117 | +
|
| 118 | + result = string.Format("The user chose: {0}", theUserChoice); |
| 119 | +
|
| 120 | + MyItem = theUserChoice; |
| 121 | + } |
| 122 | +
|
| 123 | + protected List<string> MyList = new List<string>() { "first", "second", "third" }; |
| 124 | +
|
| 125 | + protected string MyItem { get; set; } = "second"; |
| 126 | +} |
| 127 | +```` |
| 128 | + |
| 129 | + |
| 130 | +## OnChange |
| 131 | + |
| 132 | +The `OnChange` event represents a user action - confirmation of the current value/item. It is suitable for handling custom values the user can enter as if the combo box were an input. The key differences with `ValueChanged` are: |
| 133 | + |
| 134 | +* `OnChange` does not prevent two-way binding (the `@bind-Value` syntax) |
| 135 | +* `OnChange` fires when the user presses `Enter` in the input, or blurs the input (for example, clicks outside of the combo box). It does not fire on every keystroke, even when `AllowCustom="true"`, but it fires when an item is selected from the dropdown. To get the selected item, you can check if the new value is present in the data source. |
| 136 | + |
| 137 | +See the [ComboBox Overview - Selected Item]({%slug components/combobox/overview%}#selected-item) article for details on when the event fires and how item selection and `Value` work. |
| 138 | + |
| 139 | +>caption Handle OnChange without custom values - to get a value from the list, you must write text that will match the text of an item (e.g, "item 5"). |
| 140 | +
|
| 141 | +````CSHTML |
| 142 | +@result |
| 143 | +<br /> |
| 144 | +@selectedValue |
| 145 | +<br /><br /> |
| 146 | +<TelerikComboBox Data="@myComboData" TextField="MyTextField" ValueField="MyValueField" |
| 147 | + @bind-Value="@selectedValue" OnChange="@MyOnChangeHandler"> |
| 148 | +</TelerikComboBox> |
| 149 | +
|
| 150 | +@code { |
| 151 | + string result; |
| 152 | + int selectedValue { get; set; } = 3; |
| 153 | +
|
| 154 | + private void MyOnChangeHandler(object theUserInput) |
| 155 | + { |
| 156 | + // the handler receives an object that you may need to cast to the type of the component |
| 157 | + // if you do not provide a Value, you must provide the Type parameter to the component |
| 158 | + result = string.Format("The user entered: {0}", (int)theUserInput); |
| 159 | + } |
| 160 | +
|
| 161 | + public class MyComboModel |
| 162 | + { |
| 163 | + public int MyValueField { get; set; } |
| 164 | + public string MyTextField { get; set; } |
| 165 | + } |
| 166 | +
|
| 167 | + IEnumerable<MyComboModel> myComboData = Enumerable.Range(1, 20).Select(x => new MyComboModel { MyTextField = "item " + x, MyValueField = x }); |
| 168 | +} |
| 169 | +```` |
| 170 | + |
| 171 | +>caption Handle OnChange with custom values - the event fires on blur or enter |
| 172 | +
|
| 173 | +````CSHTML |
| 174 | +@result |
| 175 | +<br /> |
| 176 | +@selectedValue |
| 177 | +<br /><br /> |
| 178 | +<TelerikComboBox Data="@myComboData" TextField="MyTextField" ValueField="MyValueField" |
| 179 | + @bind-Value="@selectedValue" OnChange="@MyOnChangeHandler" AllowCustom="true"> |
| 180 | +</TelerikComboBox> |
| 181 | +
|
| 182 | +@code { |
| 183 | + string result; |
| 184 | + string selectedValue { get; set; } = "3"; |
| 185 | +
|
| 186 | + private void MyOnChangeHandler(object theUserInput) |
| 187 | + { |
| 188 | + // the handler receives an object that you may need to cast to the type of the component |
| 189 | + // if you do not provide a Value, you must provide the Type parameter to the component |
| 190 | + result = string.Format("The user entered: {0}", (string)theUserInput); |
| 191 | + } |
| 192 | +
|
| 193 | + public class MyComboModel |
| 194 | + { |
| 195 | + public string MyValueField { get; set; } |
| 196 | + public string MyTextField { get; set; } |
| 197 | + } |
| 198 | +
|
| 199 | + IEnumerable<MyComboModel> myComboData = Enumerable.Range(1, 20).Select(x => new MyComboModel { MyTextField = "item " + x, MyValueField = x.ToString() }); |
| 200 | +} |
| 201 | +```` |
| 202 | + |
| 203 | + |
| 204 | +## See Also |
| 205 | + |
| 206 | +* [ValueChanged and Validation]({%slug value-changed-validation-model%}) |
0 commit comments