Skip to content

Commit 5b8b27f

Browse files
docs(dropdownlist): separate events article
1 parent 75d64a5 commit 5b8b27f

File tree

2 files changed

+84
-43
lines changed

2 files changed

+84
-43
lines changed

components/dropdownlist/events.md

Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
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%})

components/dropdownlist/overview.md

Lines changed: 1 addition & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ To use a Telerik DropDownList for Blazor
1919
1. set the `TextField` and `ValueField` properties to point to the corresponding names of the model
2020
1. set the `Value` property to the intial value of the model (optional).
2121

22-
>caption Basic dropdownlist [data binding](data-bind) and value binding
22+
>caption Basic dropdownlist [data binding](data-bind) and two-way value binding
2323
2424
````CSHTML
2525
Selected value: @selectedValue
@@ -108,48 +108,6 @@ The DropDownList provides the following features:
108108

109109
## Examples
110110

111-
>caption Handling the ValueChanged event and providing an initial value
112-
113-
````CSHTML
114-
@result
115-
<br />
116-
@InitialValue
117-
<br />
118-
119-
<TelerikDropDownList Data="@myDdlData" TextField="MyTextField" ValueField="MyValueField"
120-
Value="@InitialValue" ValueChanged="@( (int v) => MyValueChangedHandler(v) )">
121-
</TelerikDropDownList>
122-
123-
@code {
124-
IEnumerable<MyDdlModel> myDdlData = Enumerable.Range(1, 20).Select(x => new MyDdlModel { MyTextField = "item " + x, MyValueField = x });
125-
126-
int InitialValue { get; set; } = 3; // an intial value is not required, this example showcases how to set it
127-
128-
string result { get; set; }
129-
130-
public class MyDdlModel
131-
{
132-
public int MyValueField { get; set; }
133-
public string MyTextField { get; set; }
134-
}
135-
136-
async Task MyValueChangedHandler(int newVal)
137-
{
138-
// the type of the value field in the model determines the signature of the handler
139-
result = $"The user selected {newVal}";
140-
141-
// handling ValueChanged does not let you use value binding, so if you need to update the model
142-
// you must do that manually in the handler. This is not required, though
143-
InitialValue = newVal;
144-
}
145-
}
146-
````
147-
148-
@[template](/_contentTemplates/common/general-info.md#event-callback-can-be-async)
149-
150-
@[template](/_contentTemplates/common/issues-and-warnings.md#valuechanged-lambda-required)
151-
152-
153111
>caption Get selected item from external code
154112
155113
````CSHTML

0 commit comments

Comments
 (0)