|
| 1 | +--- |
| 2 | +title: Events |
| 3 | +page_title: DropDownList for Blazor | Events |
| 4 | +description: Events in the DropDownList for Blazor |
| 5 | +slug: components/dropdownlist/events |
| 6 | +tags: telerik,blazor,dropdown,list,dropdownlist,events |
| 7 | +published: true |
| 8 | +position: 20 |
| 9 | +--- |
| 10 | + |
| 11 | +# Events |
| 12 | + |
| 13 | +This article explains the events available in the Telerik DropDownList for Blazor: |
| 14 | + |
| 15 | +* [ValueChanged](#valuechanged) |
| 16 | +* `OnChange` - inherited event that you should not use, but you may see in the intellisense |
| 17 | + |
| 18 | + |
| 19 | +## ValueChanged |
| 20 | + |
| 21 | +The `ValueChanged` event fires upon every change of the user selection. |
| 22 | + |
| 23 | +>note If the initial `Value` is not present in the data source, the component will select the first item of the data source and this will fire the event as well. If you do not update the field from which the `Value` is taken, you may end in an infinite loop. This scenario is most common when the initial value is `null` as data sources rarely have items with a `null` value. |
| 24 | +
|
| 25 | +The examples below use [binding]({%slug components/dropdownlist/databind%}) to primitive types for brevity, you can use full models as well. |
| 26 | + |
| 27 | +>caption Handle ValueChanged |
| 28 | +
|
| 29 | +````CSHTML |
| 30 | +@result |
| 31 | +<br /> |
| 32 | +<TelerikDropDownList Data="@MyList" ValueChanged="@( (string v) => MyValueChangeHandler(v) )"> |
| 33 | +</TelerikDropDownList> |
| 34 | +
|
| 35 | +@code { |
| 36 | + string result; |
| 37 | +
|
| 38 | + private void MyValueChangeHandler(string theUserChoice) |
| 39 | + { |
| 40 | + result = string.Format("The user chose: {0}", theUserChoice); |
| 41 | + } |
| 42 | +
|
| 43 | + protected List<string> MyList = new List<string>() { "first", "second", "third" }; |
| 44 | +
|
| 45 | + //protected string MyItem { get; set; } = "second"; |
| 46 | +} |
| 47 | +```` |
| 48 | + |
| 49 | +@[template](/_contentTemplates/common/general-info.md#event-callback-can-be-async) |
| 50 | + |
| 51 | +@[template](/_contentTemplates/common/issues-and-warnings.md#valuechanged-lambda-required) |
| 52 | + |
| 53 | +>caption Handle ValueChanged and provide initial value |
| 54 | +
|
| 55 | +````CSHTML |
| 56 | +from the handler: @result |
| 57 | +<br /> |
| 58 | +from model: @MyItem |
| 59 | +<br /> |
| 60 | +<br /> |
| 61 | +<TelerikDropDownList Data="@MyList" Value="@MyItem" ValueChanged="@( (string v) => MyValueChangeHandler(v) )"> |
| 62 | +</TelerikDropDownList> |
| 63 | +
|
| 64 | +@code { |
| 65 | + string result; |
| 66 | +
|
| 67 | + private void MyValueChangeHandler(string theUserChoice) |
| 68 | + { |
| 69 | + result = string.Format("The user chose: {0}", theUserChoice); |
| 70 | +
|
| 71 | + //you have to update the model manually because handling the ValueChanged event does not let you use @bind-Value |
| 72 | + MyItem = theUserChoice; |
| 73 | + } |
| 74 | +
|
| 75 | + protected List<string> MyList = new List<string>() { "first", "second", "third" }; |
| 76 | +
|
| 77 | + protected string MyItem { get; set; } = "second"; |
| 78 | +} |
| 79 | +```` |
| 80 | + |
| 81 | +## See Also |
| 82 | + |
| 83 | +* [ValueChanged and Validation]({%slug value-changed-validation-model%}) |
0 commit comments