Skip to content

Commit 28f32ae

Browse files
kb(combobox): clear button event
1 parent 889378b commit 28f32ae

File tree

1 file changed

+99
-0
lines changed

1 file changed

+99
-0
lines changed
Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
1+
---
2+
title: Clear button event
3+
description: How to get an event for the clear button click
4+
type: how-to
5+
page_title: Clear button event
6+
slug: combobox-kb-clear-button-click
7+
position:
8+
tags:
9+
ticketid: 1513072
10+
res_type: kb
11+
---
12+
13+
## Environment
14+
<table>
15+
<tbody>
16+
<tr>
17+
<td>Product</td>
18+
<td>ComboBox for Blazor</td>
19+
</tr>
20+
</tbody>
21+
</table>
22+
23+
24+
## Description
25+
Is there any event that fires when you click the clear button on the combox box?
26+
27+
There doesn't seem to be any event attached to the clear action, I would like to catch this event to reset the list, but there is nothing to catch it appear until you click outside of the box (Blur).
28+
29+
## Solution
30+
You can use the `ValueChanged` event for an immediate response, or the `OnChange` event if it is OK for you to wait for the user to confirm this choice (it happens when they press Enter or blur the input).
31+
32+
What you need to do is to check if the new value that comes in is the `default` for the value type. The code snippet below showcases two ways to do that for different binding scenarios and value types.
33+
34+
>caption Get an event for the Clear button
35+
36+
````CSHTML
37+
Monitor the console to see when the events fire in these examples. You can use only one of the events, this sample showcases both so you can see the options.
38+
39+
<TelerikComboBox Data="@MyList" ClearButton="true"
40+
Value="@MyItem" ValueChanged="@( (string v) => MyValueChangeHandlerString(v) )"
41+
OnChange="@OnChangeHandlerString">
42+
</TelerikComboBox>
43+
44+
@code {
45+
string MyItem { get; set; } = "second";
46+
void MyValueChangeHandlerString(string theUserChoice)
47+
{
48+
//you have to update the model manually because handling the ValueChanged event does not let you use @bind-Value
49+
//if you do not use ValueChanged, you can keep using two-way binding
50+
MyItem = theUserChoice;
51+
52+
Console.WriteLine($"ValueChanged string: user cleared the input: {string.IsNullOrEmpty(MyItem)}");
53+
}
54+
55+
void OnChangeHandlerString(object newValue)
56+
{
57+
Console.WriteLine($"OnChange string: user cleared the input: {MyItem == default(string)}");
58+
}
59+
60+
List<string> MyList = new List<string>() { "first", "second", "third" };
61+
62+
}
63+
64+
<hr />
65+
66+
<TelerikComboBox Data="@myDdlData" TextField="MyTextField" ValueField="MyValueField" ClearButton="true"
67+
Value="@selectedValue" ValueChanged="@( (int v) => MyValueChangeHandlerInt(v) )"
68+
OnChange="@OnChangeHandlerInt">
69+
</TelerikComboBox>
70+
71+
@code {
72+
int selectedValue { get; set; } = 2;
73+
74+
void MyValueChangeHandlerInt(int theUserChoice)
75+
{
76+
//you have to update the model manually because handling the ValueChanged event does not let you use @bind-Value
77+
//if you do not use ValueChanged, you can keep using two-way binding
78+
selectedValue = theUserChoice;
79+
80+
Console.WriteLine($"ValueChanged int: user cleared the input: {selectedValue == default(int)}");
81+
}
82+
83+
void OnChangeHandlerInt(object newValue)
84+
{
85+
Console.WriteLine($"OnChange int: user cleared the input: {selectedValue == default(int)}");
86+
}
87+
88+
IEnumerable<MyDdlModel> myDdlData = Enumerable.Range(1, 20).Select(x => new MyDdlModel { MyTextField = "item " + x, MyValueField = x });
89+
90+
public class MyDdlModel
91+
{
92+
public int MyValueField { get; set; }
93+
public string MyTextField { get; set; }
94+
}
95+
}
96+
````
97+
98+
## See Also
99+
[ComboBox Events]({%slug components/combobox/events%})

0 commit comments

Comments
 (0)